Question

Specifically I want to call {% if typeof(myvar) == "boolean" %} or {% set a = parseInt(myvar) %}, but now it gives this error:

SyntaxError: Unexpected token !== at Object.Function () at Object.precompile (/home/aidan/Desktop/nodejs/turbo/node_modules/swig/lib/swig.js:465:11) at Object.compile (/home/aidan/Desktop/nodejs/turbo/node_modules/swig/lib/swig.js:560:16) at Function.exports.swig.render (/home/aidan/Desktop/nodejs/turbo/node_modules/consolidate/lib/consolidate.js:246:56) at /home/aidan/Desktop/nodejs/turbo/node_modules/consolidate/lib/consolidate.js:146:25 at /home/aidan/Desktop/nodejs/turbo/node_modules/consolidate/lib/consolidate.js:99:5 at fs.js:266:14 at Object.oncomplete (fs.js:107:15)

Was it helpful?

Solution

You can define a filter and then write a set statement with the filter applied to the variable. Finally you can use this variable in your if statement.

E.g.:

swig.setFilter('isObject', function(element) {
  return typeof element == "object";
});

Then:

{% set elementIsObject = myvar|isObject %}
{% if elementIsObject %} ... {% endif %}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top