nunjucksgulp-nunjucks-render

How to wrap HTML to If Expression statements in Nunjucks


Based on the following docs, I can easily make use of the If Expressions as exampled. But how can we take them further by wrapping HTML to the object? I want to do something like this...

{{ "<li>" . foo . "</li>" if foo else '' }}

what would be the proper syntax?


Solution

  • As I have recently discovered, one way to go about this process is to use the Join and Safe filters to help achieve this goal. The following works for me today.

    
      {% set desc = ["<li class='m-y-sm'>", event.description, "</li>" ] | join | safe %}
    
      <ul>
        <li><p>guaranteed content</p></li>
        {{ desc if event.description else ''}}
      </ul>