Search code examples
htmlhandlebars.jsassemble

Set a new Variable in Assemble (or combine two comparison helpers)?


is there a way to set a new variable in a partial in assemble (assemble.io)? Or is it possible to combine two comparison-helpers, for example:

{#is somevar "yes" || anothervar "no"}

There's HTML in my partial which should only be shown if one of two different variables is true. If there's only one variable, i only write {{#is somevar "yes"}}, but i need that #is-helper for both variables.

I haven't found a way in the assemble-docs so i tried another way. I tried to do two #is-Helper like:

{{#is somevar "yes"}}
  // HTML
{{/is}}
{{#is anothervar "no"}}
  // HTML (the same as above)
{{/is}}

But this is really redundant... so i tried to set a variable inside both #is, for example:

{{checkvar = 0}}
{{#is somevar "yes"}}
  {{checkvar = 1}}
{{/is}}
{{#is anothervar "no"}}
  {{checkvar = 1}}
{{/is}}

So i only have to check for one variable (checkvar).

BUT all this wont work... So is there a way to do this in assemble?

Greetings


Solution

  • This would be something common to any Handlebars templates and not specifically Assemble.

    Assemble uses handlebars-helpers and we've recently upgraded to Handlebars 3.0 so you can use a combination of subexpressions with the or and is helpers:

    {{#or (is somevar "yes") (is anothervar "no")}}
      <div>somevar = yes or anothervar = no</div>
    {{/or}}
    

    Edit: This might not work since the is helper is a block helper. You might have to write your own helper to just return the value. Either that or change somevar and anothervar to be truthy values instead of yes and no. Then you can just use the or helper directly:

    {{#or somevar anothervar}}
    {{/or}}