fastify / point-of-view

Template rendering plugin for Fastify
MIT License
341 stars 86 forks source link

Multiple Templates Directory (with Template Inheritance) #49

Closed exequiel09 closed 3 years ago

exequiel09 commented 6 years ago

Is multiple templates directory like the one below supported? Since some of the template engines supported allows extension of templates (template inheritance) from other templates (e.g. nunjucks).

root
└───modules
│   │   └───home
│   │   │   │   index.js
│   │   │   └───templates
│   │   │   │   └───home
│   │   │   │   │   │   home-template1.html (inherits from base.html)
│   │   │   │   │   │   home-template2.html (inherits from base.html)
│   │   └───about
│   │   │   │   index.js
│   │   │   └───templates
│   │   │   │   └───about
│   │   │   │   │   │   home-template1.html (inherits from base.html)
│   │   │   │   │   │   home-template2.html (inherits from base.html)
└───templates
│   │   base.html
mcollina commented 6 years ago

I don't think so. However, you can instantiate point-of-view in separate plugins. So you can have different part of the application using different point-of-view configuration.

Anyway, would you like to send a PR?

exequiel09 commented 6 years ago

Editing the title since it causes confusion. The thing is you wont get to access the top-most templates dir which you can use to place the base template and extend it from the other modules.

I don't think so. However, you can instantiate point-of-view in separate plugins. So you can have different part of the application using different point-of-view configuration.

This would require a lot of refactoring since templatesDir requires only string and other templating engines have a different mechanism on how inheritance in template works. The above structure is somewhat similar to Flask Blueprints in Python.

This type of template inheritance in Flask is feasible with nunjucks (correct me if i'm wrong in other templating engines) since it was entirely inspired by Python's jinja2 package.

exequiel09 commented 6 years ago

Templating Engines (Template Inheritance and Template Inclusion) Summary

This table shows the templating engines currently supported alongside with their support for template inheritance and template inclusion. (This list is just based on my research and may contain errors)

Template Engine Template Inheritance Template Inclusion
ejs No Yes
ejs-mate Yes Yes
nunjucks Yes Yes
pug Yes Yes
handlebars No Yes
marko No Yes
mcollina commented 6 years ago

This would be a very useful feature. Would you like to file a PR with this?

exequiel09 commented 6 years ago

Sorry @mcollina i was busy this past few days. I can't as of the moment so many tasks to do. Will just leave it here open so anyone can look it into it. If i have time i'll just bring it up here that i have filed a PR.

dtex commented 6 years ago

Working on a PR for Mustache which supports template inclusion ("partials" in mustache-land) and am considering how best to handle this. I have a proposal and a question:

  1. Most of the view signatures in POV are viewEngineName(pagePath, data) except for Marko and now Mustache which both are viewEngineName(pagePath, data, options). The only available property on Marko options (currently) is stream and the only one on Mustache is partials. I propose that these two properties be the starting point for defining a common, optional options parameter that is the same for all engines. Not all engines will support all the properties of course but it will give us a common API.
  2. When view is called the templates are loaded from disk via fs. With partials (included templates) we could be loading several or even dozens of template files on every view. Would it be better to leverage the existing LRU cache when loading template files instead of always depending on fs?
mcollina commented 6 years ago

I'm +1 for both proposals.

cemremengu commented 6 years ago

Did mustache support solved this issue?

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.