How can I conditionally add a Handlebars partial to an Assemble layout?

筅森魡賤 提交于 2020-01-25 01:53:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!