如何有条件地将“把手"部分添加到“装配"布局中? [英] How can I conditionally add a Handlebars partial to an Assemble layout?
问题描述
我正在使用组装和车把以生成静态网站.我有一些页面使用相同的一般布局,但有一些例外,其中需要添加主{{> body}}
之外的内容.
I'm using Assemble along with Handlebars to generate a static site. I have a handful of pages that use the same general layout, with a couple of exceptions where content outside of the main {{> body}}
needs to be added.
给出以下通用页面结构:
Given this generic page structure:
<html>
<body>
<section>{{> body}}</section>
<!-- global scripts -->
</body>
</html>
index.html
并且需要在<!-- global scripts -->
和</body>
之间添加标记,最好的方法是什么?
and the need for index.html
to have markup added between <!-- global scripts -->
and </body>
, what's the best way to go about it?
如果像这样在我的Gruntfile中包含部分内容:
If I include a partial in my Gruntfile like so:
index: {
options: {
partials: ['source/partials/home/**/*.hbs'],
},
files: [{
expand: true,
cwd: 'source/pages/',
src: '**/index.hbs',
dest: 'output/'
}],
},
此部分可用于assemble
任务中的所有其他后续项目,我当然不希望这样做.如果这只是一个变量,我可以在YML前端或类似的东西中进行操作,但是它是HTML和内联JS的一部分,因此比我想放在前端的内容要多.我也无法通过JS加载此标记,因为它需要被阻止.
this partial is available to all other subsequent items in the assemble
task, which I certainly don't want. If this was simply a variable, I could do it in YML front matter or something like that, but it's a block of HTML and inline JS and thus is a bit more than I would want to put in front matter. I also cannot load this markup via JS as it needs to be blocking.
推荐答案
您可以将变量添加到需要部分包含在内的页面的前部,例如:
You could add a variable to the frontmatter of the pages that need the partial included, like this:
specialHTML: true
然后添加您的代码
{{#if specialHTML}}
{{> mypartial }}
{{/specialHTML}}
因此,仅当您在前题中设置"SpecialHTML"时,才会添加部分内容.
so the partial would only be added if you set the "SpecialHTML" in the frontmatter.
这篇关于如何有条件地将“把手"部分添加到“装配"布局中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!