Search code examples
jekyllliquid

How can I unset a variable in liquid?


How can I unset a variable in liquid, that has been created via an earlier {% assign ... %} statement?


Solution

  • In Liquid, you won't find a command like "unset" that does what you want directly.

    However, you can make a variable empty and and effectively non-existent using the value nil:

    {% assign foo = nil %}

    In a boolean expression, a variable with the value nil is considered false.

    In the example below Liquid will print nothing:

    {% assign foo = nil %}
    {% if foo %}
      <p>I'm a paragraph.</p>
    {% endif %}
    

    You can find more info here.