Jade/Pug 中带有动态数据的部分模板

kap*_*ell 3 javascript partials node.js pug

我正在尝试用 2 个块创建视图。每个块都有不同的实时数据源。当我在主视图中使用 Jade 的包含时,:

extends ../layout

block content
 link(rel='stylesheet', type='text/css', href='/stylesheets/people.css')
 include ../store/peopleTemplate.pug
Run Code Online (Sandbox Code Playgroud)

我收到错误

Cannot read property 'people' of undefined.
Run Code Online (Sandbox Code Playgroud)

原因是数据仍在加载中。如果排除该包含,而是在恢复数据使用的函数中

res.render(template, {  data:localData });
Run Code Online (Sandbox Code Playgroud)

模板不会添加到视图中。

如何将来自不同来源的动态数据的 2 个或多个部分视图添加到 1 个视图?谢谢

小智 5

你可以通过 mixins 来实现这一点。

布局.pug

doctype html
html
  head
    ...
  body
    block content
Run Code Online (Sandbox Code Playgroud)

宠物部分.pug

mixin petslist(pets)
  ul
    each pet in pets
      li #{pet}
Run Code Online (Sandbox Code Playgroud)

宠物哈巴狗

extends layout

include pets-partial

block content
  h1 Dogs
  +petslist(dogs)

  h1 Cats
  +petslist(cats)
Run Code Online (Sandbox Code Playgroud)

https://pugjs.org/language/mixins.html

jade 中的语法与 pug 2 中略有不同。