开发者

expressjs: how to extend part of layout

I have layout.jade, which contains #header #main #footer, and #main contains #content and #sidebar, my template body goes to #content if I do res.rend('template'),

but now some page is special, which only want to inherit #header #main #footer and template body should goes to #main, how开发者_开发百科 can I do this in expressjs, my template engine is jade.


Jade allows you to replace (default), prepend, or append blocks. Suppose for example you have default scripts in a "head" block that you wish to utilize on every page, you might do this:

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

Now suppose you have a page of your application for a JavaScript game, you want some game related scripts as well as these defaults, you can simply append the block:

 extends layout

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


If you wish to have a special layout template for any view you just need to call de template render with the value of layout and the name and location of your new template layout.

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

This will render the content of page layout with mylayout for the #header, #footer and other stuff.


I think I'd look into view partials and organize your template so that the inner parts that will change can be swapped out.

http://expressjs.com/guide.html#view-partials

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜