Search code examples
javascriptnode.jsexpressswig-template

Swig template, testing condition


how can i do something like this with swig-template?

var remId = $(this).attr('remId');
if (remId) {
    var end = remId.search('_');
    var underscore = remId.slice(end, end + 1); 
    var Id = remId.slice(end + 1, remId.length);
}

for example, if remId = 123455_instock_white i need to find out if remId has instock or prestock.

so if Id contains instock, Id will go under instock table and if Id contains prestock, Id will go under prestock table.

any idea? how shold i filter this?


Solution

  • You can use most of what is available in base JavaScript. If you know that remId will be a string, use indexOf to test if instock or prestock is in the string.

    {% if remId.indexOf('instock') !== -1 %}
        remId has instock
    {% elif remId.indexOf('prestock') !== -1 %}
        remId has prestock
    {% endif %}