javascriptconditional-operatornunjucks

nunjucks ternary if expression


I'm using nunjucks as a templating engine in my application and can't seem to get the block to display at all when I apply more than two conditions e.g.

{{ "true" if foo else "false" }}

The above example works just fine, however the following does not:

{{ "message one" if condition_1 else "message two" if condition_2 else "default" }}

The error message I get tells me that a comma is missing but no matter where I add one it continues to tell me a comma is missing. Is there a way to get this to work? I could solve this with if tags, but I'd have to then duplicate the whole block which doesn't sound like a good solution.


Solution

  • {{ "foo" if cond1 else ("bar" if cond2 else "baz") }}
    

    should work according to this post.

    Haven't tried it myself though