Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upHandlebarsTemplateEngine: include/inherit is done wrong #514
Comments
|
@mrnejc our CI is green and I can build it locally. What are you experiencing? |
|
edit: deleted - I was building it wrong :) |
|
did a compile with -DskipTests=true loader works same way with 3.4.0-SNAPSHOT |
|
The quick and dirty solution I came to: on 1st call of Loader.sourceAt its called from TemplateHandler, but if template uses inheritance every consecutive call to method is from Handlebars library that only knows relative paths. So the solution is to save template path on 1st call and prepend it to template filename if needed. The solution works with default path templates or setting absolute path to templates. But this is dirty hack and I don't quite like it. |
|
If there are more than 1 root templates then you have no idea to which template the base path applies... |
|
No, I save just the to template path, not including root template name. i.e. the fix only applies to templates that are included in master templates (calls from Handlebars library) since they are provided with relative path. this is why I think the solution is a bit ugly: |
|
The upstream FileTemplateLoader requires an explicit base path: https://github.com/jknack/handlebars.java/blob/1f6c48e606dc1303d1e92a0a0eaa94120eba64fd/handlebars/src/main/java/com/github/jknack/handlebars/io/FileTemplateLoader.java#L42 Without breaking the current API, it seems like enforcing one in Vert.x is infeasible and that the proposed guessing-on-first-initialize is the way to go. Unless we can change templateEngine to pass in templateDirectory, or alternatively, subtract the string in the Handlebars engine. |
|
I spent some more time debugging this, and it looks like it won't be too difficult to fix this in HandlebarsTemplateEngineImpl#render using the subtraction method as I mentioned above. |
Version
Context
Template files (*.hbs files) are put into templates directory.
When including template partial into master template or doing template inheritance the reference to another template must also include templates path. This is wrong.
Per documentation at jknack.github.io/handlebars.java including another template in same directory shouldn't require providing paths. See example with header.hbs, footer.hbs and home.hbs.
Do you have a reproducer?
Example project at https://github.com/mrnejc/vertx-web-handlebars-test. HandlebarsTest.java has main() method, starting server on port 8080. Visiting http://localhost:8080/partial displays rendered template.
Example has 3 templates in directory src/main/resources/templates/.
The file partial.hbs references main.hbs as {{> templates/main }} but it should be {{> main }} only. main.hbs also references include.hbs with {{> templates/include }} but should be {{> include }}.
Extra
I see that Loader is changed in current master, but can't test because project doesn't build for me.