我正在构建一个网站,我希望为大多数页面(但不是全部)提供布局包装。我想做的是使用布局包装器的部分内容,然后将其他内容部分传递到此主布局中。
layout.hbs
的内容
<!DOCTYPE html>
<html>
{{> head }}
<body>
{{> nav}}
{{ content }}
</body>
</html>
然后在somefile.hbs
{{> layout myPartial.hbs}}
我正在用 gulp 撕裂我的 Handlebars 模板。
如果我将标记直接传递到 layout.hbs
中,我就可以实现此功能,但我想做的是将另一个部分文件的内容传递到布局包装器中。
{{> layout content="<div>foo</div>"}} // Renders ok
还有其他方法可以实现全局布局包装吗?
最佳答案
我能够使用 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>
关于javascript - Handlebars 部分位于部分内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681560/