用玉来渲染单块
我正在与Node.js和expression。 假设我有一对玉文件:
template.jade
html body block page-content
example.jade
extends template block page-content p | Lorem ipsum yadda yadda
如果我渲染example.jade,我会得到将该段落标记插入到template.jade的body标记中的结果,这通常是我想要的。
我的问题是,我试图使用pushState和历史API来加载这些文件(以及明显不是这些文件),当这样做,我想要一个请求,只是返回页面内容块的内容本身,没有完整的HTML文件的其余部分。 有没有简单的方法告诉Jade只渲染块本身,而不是embedded到模板中?
我能想到的最好的办法是将其改为:
example.jade
extends template block page-content import example-content
例如-content.jade
p | Lorem ipsum yadda yadda
但是创build这样的额外文件似乎令人费解。
Jade支持可执行代码。 当使用前缀 – (无缓冲)。 例如,你可以使用主布局玉模板中的统计:
- if (renderType && renderType == 'page') { doctype 5 html head title= title body - } block content
渲染html页面如:
res.render('index', { title: 'StackOverflow', renderType: 'page' });
渲染html块,如:
res.render('index', { title: 'StackOverflow', renderType: 'block' });
如果您不喜欢在所有渲染expression式中使用renderTypevariables,请使用
res.locals(obj) //*Assign several locals with the given obj. The following are equivalent:*
并为所有请求设置默认值。 你在哪里启动应用程序添加处理
var logger = function(req, res, next) { res.locals({renderType:'page'}); next(); }; app.use(logger);