Search code examples
javascripttemplate-engine

Is it possible to create variables in the doT.js templating engine?


I am using doT.js, a javascript templating engine. I would like to know if it is possible to use (helper-) variables like in the following example:

{{var foo = "bar"}}

<div>{{=foo}}</div> 

Output:

<div>bar</div>

The above example does not work, but maybe there is another way. There doesn't seem to be any information about variables in the doT.js documentation.


Solution

  • There is a feature called 'compile-time defines' stated in 'default delimiters' of document. http://olado.github.com/doT/index.html

    Examples can be found in https://github.com/olado/doT/blob/master/examples/advancedsnippet.txt

    Here's the solution of your problem:

    {{##def.foo="bar"#}}
    
    <div>{{#def.foo}}</div>