pythonhtmldjangodjango-cms

Django CMS - check if placeholder is empty


I use:

I would like to check if my placeholder is empty.

<div>
    {% placeholder "my_placeholder" or %}
    {% endplaceholder %}
</div>

I don't want the html between the placeholder to be created if the placeholder is empty.

{% if placeholder "my_placeholder" %}
<div>
    {% placeholder "my_placeholder" or %}
    {% endplaceholder %}
</div>
{% endif %}

Solution

  • There is no built-in way to do this at the moment in django-cms, so you have to write a custom template tag. There are some old discussions about this on the django-cms Google Group:

    Based on the code in the first discussion, I've put together the following Gist:

    I use it like so:

    {% load extra_cms_tags %}
    {% get_placeholder "My Placeholder" as my_placeholder %}
    
    {% if my_placeholder %}
    <div>
        {{ my_placeholder }}
    </div>
    {% endif %}