Django CMS - check if placeholder is empty

后端 未结 5 2004
遥遥无期
遥遥无期 2021-02-14 04:16

I use:

  • DjangoCMS 2.4
  • Django 1.5.1
  • Python 2.7.3

I would like to check if my placeholder is empty.

{
5条回答
  •  暖寄归人
    2021-02-14 04:44

    Depending on what you are trying to achieve, you can simply use CSS to hide the element if doesn't have content using the :empty selector. And if you are worried about white spaces you can use Django's in-build {% spaceless %} template tag to remove them.

    So you'd get this template:

    {% spaceless %}
    
    {% placeholder "my_placeholder" %}
    {% endspaceless %}

    And this CSS:

    hide_if_empty:empty {
        display: none;
    }
    

    Not exactly what was asked for as it doesn't remove the HTML - but this will solve the most common case where one wants to check if a place holder is empty, and doesn't require the introduction of a new template tag.

提交回复
热议问题