English 中文(简体)
expressjs:如何扩展部分布局
原标题:expressjs: how to extend part of layout

我有layout.jade,它包含#header#main#foot,而#main包含#content#侧边栏。如果我执行res.rend(template)的操作,我的模板主体将转到#1ontent

但现在有些页面很特殊,只想继承#header#main#footer,模板主体应该转到#main中,我怎么能在expressjs中做到这一点,我的模板引擎是jade。

问题回答

Jade允许您替换(默认)、预置或附加块。例如,假设您希望在每个页面上使用的“head”块中有默认脚本,您可以这样做:

html
 head
  block head
    script(src= /vendor/jquery.js )
    script(src= /vendor/caustic.js )
 body
  block content

现在假设你有一个JavaScript游戏的应用程序页面,你想要一些与游戏相关的脚本以及这些默认值,你可以简单地附加块:

 extends layout

 append head
  script(src= /vendor/three.js )
  script(src= /game.js )

如果您希望为任何视图都有一个特殊的布局模板,您只需要使用布局的值以及新模板布局的名称和位置来调用反模板渲染。

res.render( page , { layout:  mylayout  });

这将用mylayout为#页眉、#页脚和其他内容呈现页面布局的内容。

我想我应该查看视图局部并组织你的模板,以便可以交换出将要更改的内部部分。

http://expressjs.com/guide.html#view-部分





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签