Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign variables in template #476

Closed
rottmann opened this issue Mar 29, 2013 · 14 comments
Closed

Assign variables in template #476

rottmann opened this issue Mar 29, 2013 · 14 comments

Comments

@rottmann
Copy link

Is it possible to assign a variable within a template and use it like variables from outside?

Example:
I want output a table, but 1 column is optional, depend on data (fields[].field2) exists or not.

In each row it is no problem to check, but how to check it for the header ?

<table>
  <thead>
    <tr>
      <th>Head 1</th>
      <th>Head 2 (optional)</th>
      <th>Head 3</th>
    </tr>
  </thead>
{{#each fields}}
  <tr>
    <td>{{{field1}}}</td>
    {{#if field2}}<td>{{{filed2}}}</td>{{/if}}
    <td>{{{field3}}}</td>
  </tr>
{{/each}}
</table>

At the moment i use a unsteady helper function "assign" which creates helpers.

{{#each fields}}
  {{#if field2}}
    {{assign "helperVar" "<th>Head 2 (optional)</th>"}}
  {{/if}}
{{/each}}

Output with: {{{helperVar}}}

But thats not a good solution and i cant use it in #if.

@kpdecker
Copy link
Collaborator

This goes against the premise of logicless templates and I don't think that this should be language-level construct.

I think the recommended approach here would be to calculate this data in your context creation logic and pass it into the template. This allows for much more control as you have the full javascript language at your disposal rather than whatever subset is implemented in the template language.

template({
  fields: fields,
  hasField2: _.any(fields, function(item) { return item.field2; })
});

@tnguyen14
Copy link

I think this would be a good feature, not to introduce more logic to the template, but to pass in variables that is specific to that template.

For example, 2 of my templates use the same partial. I would like to be able to pass some variables to that partial from the template level so that different things is output for the different template within the scope of the partial.

@iby
Copy link

iby commented Sep 16, 2014

+1

2 similar comments
@giyyapan
Copy link

+1

@koemeet
Copy link

koemeet commented Dec 10, 2014

👍

@efuquen
Copy link

efuquen commented Mar 30, 2015

👍

@kpdecker We can have loops, if statements, and a number of other logic constructs in templates. But apparently assigning a variable is too much logic in templates? I really don't get the reasoning behind that decision when the other default helpers contain way more logic than simply having the ability to assign a variable.

@luisdaniel
Copy link

+1

3 similar comments
@gudoy
Copy link

gudoy commented Jun 25, 2015

+1

@booleangate
Copy link

+1

@miquelescolar
Copy link

+1

@kpdecker
Copy link
Collaborator

kpdecker commented Aug 9, 2015

Something along these lines can be implemented using block parameters and is documented here:
http://handlebarsjs.com/block_helpers.html#block-params

Since this is done in a separate namespace outside of the current context, it provides more consistent behavior over context assignment.

@paquinmathieu
Copy link

paquinmathieu commented Oct 7, 2021

Something along these lines can be implemented using block parameters and is documented here: http://handlebarsjs.com/block_helpers.html#block-params

Since this is done in a separate namespace outside of the current context, it provides more consistent behavior over context assignment.

the link does not work
are you refering to this?
https://handlebarsjs.com/guide/block-helpers.html#block-parameters

@swport
Copy link

swport commented Feb 13, 2022

only the lord knows why some people think it's a good idea to use hbs at the server with node.

@alexv-ds
Copy link

alexv-ds commented Jan 8, 2024

only the lord knows why some people think it's a good idea to use hbs at the server with node.

why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests