问题
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>
and the need for index.html
to have markup added between <!-- global scripts -->
and </body>
, what's the best way to go about it?
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/'
}],
},
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.
回答1:
You could add a variable to the frontmatter of the pages that need the partial included, like this:
specialHTML: true
and then add in your code
{{#if specialHTML}}
{{> mypartial }}
{{/specialHTML}}
so the partial would only be added if you set the "SpecialHTML" in the frontmatter.
来源:https://stackoverflow.com/questions/24064912/how-can-i-conditionally-add-a-handlebars-partial-to-an-assemble-layout