问题
Is there a good way of doing templating (that allows me to write html -- not jade), that will support layouts and partials?
I have only been able to use ejs with express-partials npm plugin to get this behavior. However I feel the ejs site is out of date and not well supported.
I'm looking for good examples.
I want to create a main layout that would have header + footer and include a separate template for the nav for example.
Knockoutjs looks well supported, but I cannot find many examples of using it with express.
Update: I found a pretty good article here from LinkedIn's evaluation of js templates: http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more
回答1:
You can easily mimic this with the "include" feature in EJS. It's not quite as elegant as layouts in Express 2.x but it works for simple scenarios where all you need is a header and a footer plus you don't need to use Jade.
Basically you can create two "layout" pages (layoutTop.ejs and layoutBottom.ejs) and manually include them on every page.
mypage.ejs
include layoutTop
html for my page.ejs goes here
include layoutBottom
(Shameless plug) You can see a more detailed explanation here: http://hectorcorrea.com/Blog/Using-layouts-with-EJS-in-Express-3.x
回答2:
I have settled on using visionmedia's ejs with ejs-locals for layouts, includes, and blocks.
https://github.com/RandomEtc/ejs-locals https://github.com/visionmedia/ejs
回答3:
Try twigjs. It fits your description well.
https://github.com/fadrizul/twigjs
But ejs don't looks for me unsupported either. I think that this are even solutions.
回答4:
Give handlebars a try.
You can use it with hotplates to have your templates automatically resolved.
It also supports reusing your partials client side.
来源:https://stackoverflow.com/questions/12471043/node-express-template-engine-that-supports-layouts-and-partials