Skip to content

Sub-component support #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2025
Merged

Sub-component support #35

merged 2 commits into from
Jun 13, 2025

Conversation

lucaswerkmeister
Copy link
Member

@lucaswerkmeister lucaswerkmeister commented Jun 11, 2025

See the individual commit messages. Support for callable components is not included, since that needs more work (see the app+callable branch).

if ( str_starts_with( $error->message, 'Tag template invalid' ) ) {
$msg = $error->message;
if ( str_starts_with( $msg, 'Tag ' ) && str_ends_with( $msg, " invalid\n" ) ) {
// discard "Tag xyz invalid" messages from libxml2 < 2.14.0(?)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it’s annoying that it doesn’t happen for me locally, only in CI :S

@codders
Copy link
Contributor

codders commented Jun 12, 2025

This looks good to me. It's unfortunate that we need the error workaround for libxml, but that seems like it might be unavoidable.

The App class lets us reuse the same HtmlParser and JsExpressionParser
across multiple components (note that JsExpressionParser returns an
unevaluated expression, so it’s fine to cache its results even if
they’re going to be evaluated with different data later), and also keeps
a library of components. The components are loaded and compiled
on-demand the first time they’re used, and reused afterwards; for this,
Component now clones its root node before operating on it.

For Component to work correctly, it’s important that the cloned node
still has a parent node (otherwise it’s considered to have been removed
from the DOM, and most of the processing for it is skipped); use a
cloneOwner node for that purpose. (For now, it’s just the document’s
document element, i.e. the <html>, but we could make it a separate node
later if we prefer. It just has to be outside the root node.)

(If you’re wondering, my motivation for making the components input
either string or callable is that it probably makes sense not to load
all the component files into strings upfront, but to only do that if the
component is needed.)
This is a bit odd in that Component doesn’t even know which components
are registered in the app; sub-component handling is triggered iff the
tag name contains a hyphen. I think this is reasonable, but we might
still end up changing it.

Note that the sub-component must be a descendent of the template root
note; a template cannot just consist of a call to a sub-component. (If
it does, Component::render() crashes when trying to remove the $rootNode
from the cloneOwner because it’s already been replaced in
handleComponent().)
@lucaswerkmeister lucaswerkmeister changed the title [DRAFT] Sub-component support Sub-component support Jun 12, 2025
@lucaswerkmeister lucaswerkmeister marked this pull request as ready for review June 12, 2025 13:15
Copy link
Contributor

@codders codders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great - let's give it a go!

@codders codders merged commit 4e6e484 into master Jun 13, 2025
8 checks passed
@lucaswerkmeister lucaswerkmeister deleted the app branch June 13, 2025 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants