Node.js express: confuse about ejs template

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

I put my ejs template file in views folder like:

views | |--foo.html | |--layout.html 

so I config my ejs template:

app.set('views', __dirname + '/views'); app.engine('html', require('ejs').renderFile);   

I render my foo template by this:

res.render('foo.html', {title: 'test'}); 

I want use layout.html as the master layout template, I also add <%- body%> tag in layout.html, but is doesn't work, what I saw is only return the ended foo.html.

Why the layout.html can't be the master layout?Or how can I set it to the master layout?

回答1:

Ahah you just got tricked by Express 3 change in layout management. The official example: https://github.com/visionmedia/express/tree/master/examples/ejs Has not been updated.

In Jade you now have to use blocks and extend the layout. http://www.devthought.com/code/use-jade-blocks-not-layouts/



回答2:

Try to use ejs-locals then you can do this in a login.ejs file:

<% layout('layout') -%> <form></form> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!