如何在停泊中包含的文件中使用块?

发布于 2025-02-01 21:35:17 字数 339 浏览 2 评论 0原文

我想做类似本

//- page-a.pug
extends layout.pug

block scripts
  script(src='/jquery.js')
  script(src='/pets.js')

block content
    include ./layout2.pug
        block article1
            something something....
        block article2
            something something....

pract1和present2的事情在layout2.pug文件中

I want to do something like this

//- page-a.pug
extends layout.pug

block scripts
  script(src='/jquery.js')
  script(src='/pets.js')

block content
    include ./layout2.pug
        block article1
            something something....
        block article2
            something something....

article1 and article2 are in the layout2.pug file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2025-02-08 21:35:17

目前,不允许使用多个哈巴狗中的多个块,请参阅 esjuction

但是,使用Mixin Block我们可以模拟多个块。
例如:

index.pug

//- important to use `var`, not `let` or `const`
- var blocks = blocks || {}

mixin block(name)
  -
    if (this.block && !this.block.length)
      blocks[name] = this.block
    else if (!!blocks[name]) blocks[name]()

//- define your custom block with name `article1` 
+block('article1')
  p something something 1 ...

//- define your custom block with name `article2`
+block('article2')
  p something something 2 ...

//- use the `include` after block definitions
include article

acrats.pug

h2 Articles
ul
  li
    //- output block with name `article1`
    +block('article1')
  li
    //- output block with name `article2`
    +block('article2')

生成的html:

<h2>Articles</h2>
<ul>
  <li>
    <p>something something 1 ...</p>
  </li>
  <li>
    <p>something something 2 ...</p>
  </li>
</ul>

Currently, multiple blocks in Pug are not allowed, see the issue.

But, using the mixin block we can simulate multiple blocks.
For example:

index.pug

//- important to use `var`, not `let` or `const`
- var blocks = blocks || {}

mixin block(name)
  -
    if (this.block && !this.block.length)
      blocks[name] = this.block
    else if (!!blocks[name]) blocks[name]()

//- define your custom block with name `article1` 
+block('article1')
  p something something 1 ...

//- define your custom block with name `article2`
+block('article2')
  p something something 2 ...

//- use the `include` after block definitions
include article

article.pug

h2 Articles
ul
  li
    //- output block with name `article1`
    +block('article1')
  li
    //- output block with name `article2`
    +block('article2')

Generated HTML:

<h2>Articles</h2>
<ul>
  <li>
    <p>something something 1 ...</p>
  </li>
  <li>
    <p>something something 2 ...</p>
  </li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文