Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to assign a parameter value within handlebars templates without using helpers?

I am trying to assign values within a template, the idea is to do something like this:

    {{#if author}}         {{className = 'classA'}}  <- trying to implement this line.     {{else}}         {{className = 'classB'}}     {{/if}}  <div class={{className}}></div> 

Is it possible to do so without registerHelper?

like image 323
Ravit Avatar asked Jul 14 '14 12:07

Ravit


People also ask

What is helper in handlebars?

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}

How do you make handlebars for helpers?

To create custom helpers in your theme just create a helpers. js file in the theme directory; here you can add your custom helper functions. Below there is an example of code which creates two super simple custom helpers which accept one argument: /* * Custom theme helpers for Handlebars.

How do you use unless handlebars?

You can use the unless helper as the inverse of the if helper. Its block will be rendered if the expression returns a falsy value. If looking up license under the current context returns a falsy value, Handlebars will render the warning. Otherwise, it will render nothing.


1 Answers

I needed this solution solved in the same way the original poster expected. Therefore I created a helper function to do this for me.

function setVariable(varName, varValue, options){   options.data.root[varName] = varValue; }; 

Contained within the options in the helper, is the data block, with the root of the helper variables.

I placed my variable within the root object, either assigning or overwriting with the value provided.

The helper in html looks a lot like this.

{{setVariable "thisVar" "Contents Here"}}  <div>   {{thisVar}} </div> 
like image 57
The Lazy Coder Avatar answered Sep 21 '22 19:09

The Lazy Coder