Skip to content

Added automatic discovery mechanism for partials#10

Open
BennettJames wants to merge 2 commits into
davis:masterfrom
BennettJames:partials
Open

Added automatic discovery mechanism for partials#10
BennettJames wants to merge 2 commits into
davis:masterfrom
BennettJames:partials

Conversation

@BennettJames

Copy link
Copy Markdown
Collaborator

Alright, managed to piece together something that is pretty tidy compared to what I originally outlined in #9 and seems to work fine in browser & server-side.

Basic explanation of what goes on here: before translating a template, it parses the template into an AST. This syntax tree is then recursively searched for all partials, which are then added as a dependency to the currently-parsed template. Each dependency is required and registered as a partial, and the precompiled template is exported (as before). The exact same process is applied to each partial.


A few other notes:

  • The register-partials-as-dependencies system this uses can result in a partial being registered multiple times. I don't think this will ever be an issue, as re-registering the same template twice doesn't look like has any effect on Handlebars.
  • The way it travels the AST is really rough. It looks at every single value in every object in the returned AST object. I couldn't find much by ways of official documentation on how to deal with partial discovery, and this seems to work "well enough".
  • if you think this looks good, I should probably add some documentation to the readme to clarify how partials are handled.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants