【问题标题】:handlebars partial inside of a partial车把局部在局部内部
【发布时间】:2018-09-15 19:39:24
【问题描述】:

我正在构建一个网站,我希望在其中为大多数页面(但不是全部)提供一个布局包装器。我正在尝试做的是对布局包装器使用部分内容,然后将其他内容部分传递到此主布局中。

layout.hbs的内容

<!DOCTYPE html>
<html>
    {{> head }}
    <body>
        {{> nav}}
        {{ content }}
    </body>
</html>

然后在somefile.hbs

{{> layout myPartial.hbs}}

我正在使用 gulp 渲染我的车把模板。

如果我将标记直接传递给layout.hbs,我可以让它工作,但我想做的是将另一个部分文件的内容传递给布局包装器。

{{> layout content="<div>foo</div>"}} // Renders ok

我应该以另一种方式接近全局布局包装器吗?

【问题讨论】:

  • 我一直更喜欢 pugjs 而不是车把,因为它的 template inheritance 结构。看起来有一些 add-ons 可用于添加该功能。

标签: javascript html node.js gulp handlebars.js


【解决方案1】:

我能够使用dynamic partial lookup syntax 来完成这项工作。

工作解决方案

layout.hbs的内容

<!DOCTYPE html>
<html>
    {{> head }}
    <body>
        {{> nav}}
        {{> (lookup . 'partial') }}
    </body>
</html>

someFile.hbs的内容

// Allows me to hit http://w.x.y.z/someDir/someFile
{{> layout partial='someDir/_someFile'}}

someDir/_someFile.hbs的内容

// Content injected into the layout and can include nested layouts
<h1>Some content I want to render</h1>

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2016-12-08
    • 2016-07-20
    • 2010-09-09
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多