繁体   English   中英

Node.js,Jade / Pug,如何提供全部和部分模板?

[英]Node.js, Jade/Pug, how to serve full and part of template?

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

Jade不支持条件布局切换

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

不管变量名如何,这将使页面具有布局。

方法1

一种简单的方法是在一个单独的文件中定义块内容,并将其包含在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

另一种更清洁的方式是修改您的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' });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM