如何在 PUG 3 模板中设置跨块变量?
How do I set up cross-block variables in PUG 3 templates?
我有一只哈巴狗:
extends layout.pug
block a
- var x = 'test'
//- stuff
block b
//- ..... stuff
效果很好。
但是当我尝试为 block a
和 block b
移动 - var x
东西时,我失败了。
我试过这个:
extends layout.pug
- var x = 'test'
block a
//- stuff
block b
//- ..... stuff
它说:
Only named blocks and mixins can appear at the top level of an extending template
还有这个:
- var x = 'test'
extends layout.pug
block a
//- stuff
block b
//- ..... stuff
它说:
Declaration of template inheritance ("extends") should be the first thing in the file. There can only be one extends statement per file.
Template Inheritance > Common mistakes section of the Pug docs 中提到了这个问题,以及两个可能的解决方案:
Note that only named blocks and mixin definitions can appear at the top (unindented) level of a child template. This is important! Parent templates define a page’s overall structure, and child templates can only append
, prepend
, or replace specific blocks of markup and logic. If a child template tried to add content outside of a block, Pug would have no way of knowing where to put it in the final page.
This includes unbuffered code, which can also contain markup. If you need to define variables for use in a child template, you can do so a few different ways:
- Add the variables to the Pug options object, or define them in unbuffered code in a parent template. The child template will inherit these variables.
- Define the variables in a block in the child template. Extending templates must have at least one block, or it would be empty — just define your variables there.
我有一只哈巴狗:
extends layout.pug
block a
- var x = 'test'
//- stuff
block b
//- ..... stuff
效果很好。
但是当我尝试为 block a
和 block b
移动 - var x
东西时,我失败了。
我试过这个:
extends layout.pug
- var x = 'test'
block a
//- stuff
block b
//- ..... stuff
它说:
Only named blocks and mixins can appear at the top level of an extending template
还有这个:
- var x = 'test'
extends layout.pug
block a
//- stuff
block b
//- ..... stuff
它说:
Declaration of template inheritance ("extends") should be the first thing in the file. There can only be one extends statement per file.
Template Inheritance > Common mistakes section of the Pug docs 中提到了这个问题,以及两个可能的解决方案:
Note that only named blocks and mixin definitions can appear at the top (unindented) level of a child template. This is important! Parent templates define a page’s overall structure, and child templates can only
append
,prepend
, or replace specific blocks of markup and logic. If a child template tried to add content outside of a block, Pug would have no way of knowing where to put it in the final page.This includes unbuffered code, which can also contain markup. If you need to define variables for use in a child template, you can do so a few different ways:
- Add the variables to the Pug options object, or define them in unbuffered code in a parent template. The child template will inherit these variables.
- Define the variables in a block in the child template. Extending templates must have at least one block, or it would be empty — just define your variables there.