Django CMS - check if placeholder is empty

后端 未结 5 2002
遥遥无期
遥遥无期 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:26

    Here's a very compact solution.

    Template filter:

    @register.filter('placeholder_is_empty')
    def placeholder_is_empty(request, slot):
        page = request.current_page
        placeholder = page.placeholders.get(slot=slot)
        return placeholder.cmsplugin_set.exists()
    

    Usage in template:

    {% if request|placeholder_is_empty:'myplaceholder' %}
        <h1>Here comes some content... </h1>
    {% endif %}
    
    0 讨论(0)
  • 2021-02-14 04:36

    Based on the great answer form @Philip Zedler, a solution that works for both placeholder on django-cms pages, but also on placeholders "outside of the cms".

    @register.filter()
    def placeholder_empty(page_placeholder, slot=None):
        """
        for page/slot, pass a page object, and a slot name:  
        {% if request.current_page|djangocms_misc_placeholder_empty:"content" %}
    
        for a outside page placeholder, just the placeholder object:
        {% if object.placeholderfield|djangocms_misc_placeholder_empty %}
    
        also, with:
        {% with ph_empty=object.placeholderfield|djangocms_misc_placeholder_empty %}
        """
        placeholder = None
        if isinstance(page_placeholder, Placeholder):
            placeholder = page_placeholder
        elif isinstance(page_placeholder, Page):
            page = page_placeholder
            try:
                placeholder = page.placeholders.get(slot=slot)
            except Placeholder.DoesNotExist:
                pass
        if placeholder:
            # // return not placeholder.cmsplugin_set.filter(language=get_language()).exists()
            return not placeholder.cmsplugin_set.exists()
        return False
    

    usage in template

    {% if request.current_page|placeholder_empty:'content' %}
        <h1>Fallback!</h1>
    {% endif %}
    
    

    It's in my djangocms-misc package

    0 讨论(0)
  • 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 %}
    <div class="hide_if_empty">
        {% placeholder "my_placeholder" %}
    </div>
    {% 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.

    0 讨论(0)
  • 2021-02-14 04:49

    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:

    • https://groups.google.com/forum/#!topic/django-cms/WDUjIpSc23c/discussion
    • https://groups.google.com/forum/#!msg/django-cms/iAuZmft5JNw/yPl8NwOtQW4J
    • https://groups.google.com/forum/?fromgroups=#!topic/django-cms/QeTlmxQnn3E
    • https://groups.google.com/forum/#!topic/django-cms/2mWvEpTH0ns/discussion

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

    • https://gist.github.com/timmyomahony/5796677

    I use it like so:

    {% load extra_cms_tags %}
    {% get_placeholder "My Placeholder" as my_placeholder %}
    
    {% if my_placeholder %}
    <div>
        {{ my_placeholder }}
    </div>
    {% endif %}
    
    0 讨论(0)
  • 2021-02-14 04:50

    If you want additional content to be displayed in case the placeholder is empty, use the or argument and an additional {% endplaceholder %} closing tag. Everything between {% placeholder "..." or %} and {% endplaceholder %} is rendered in the event that the placeholder has no plugins or the plugins do not generate any output.

    Example:

    {% placeholder "content" or %}
    
    There is no content.
    
    {% endplaceholder %}
    
    0 讨论(0)
提交回复
热议问题