【问题标题】:Node.js, Jade/Pug, how to serve full and part of template?Node.js,Jade/Pug,如何提供完整和部分模板?
【发布时间】:2016-09-20 08:51:11
【问题描述】:

如果请求是通过 ajax 发出的,我只想从 page1.jade 发送块内容,如果是正常的 GET,它应该使用 layout.jade 中内置的块来回答

【问题讨论】:

标签: javascript node.js pug pugjs


【解决方案1】:

翡翠不支持conditional layout switch:

if type=='get'
  extends layout
block content
  p This is block content

这将使用布局呈现页面,而与变量名称无关。

方法一

一种简单的方法是在单独的文件中定义块内容并将其包含在您的 page1.jade 中,然后您可以独立访问该块。

layout.jade

html
head
title My Site - #{title}
block scripts
body
  block content
  block foot

page1.jade

extends layout

block content
  include ./includes/block.jade

包括/block.jade

p This is the block content

这将是在你的路由文件中处理请求的方式

router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/block', function(req, res, next) {
  res.render('includes/block', { title: 'Express' });
});

修改它以处理 AJAX/浏览器请求。

方法 2

另一种 cleaner 方法是修改你的 layout.jade 本身以进行条件

layout.jade

if type=='get'
  html
  head
  title My Site - #{title}
  block scripts
body
  block content
  block foot

并在每次渲染相同页面时从路由器传递变量:

router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express',type:'get' });
});
router.get('/block', function(req, res, next) {
  res.render('index', { title: 'Block Express' });
});

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2018-06-21
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多