2023 update
Accepted answer https://mcmap.net/q/958717/-django-cms-check-if-placeholder-is-empty has issues with latest DjangoCMS.
Updated solution (credits for F.B. from DjangoCMS community).
Tested with Django CMS 4.
{% load get_placeholder %}
...
{% get_placeholder "content" as placeholder_content %}
{% if placeholder_content %}
<div class="my-2">{{ placeholder_content }}</div>
{% endif %}
code of the templatetag .../templatetags/get_placeholder.py
from classytags.arguments import Argument, MultiValueArgument
from classytags.values import StringValue
from django import template
from django.utils.safestring import mark_safe
from cms.models.placeholdermodel import Placeholder as PlaceholderModel
from cms.templatetags.cms_tags import (
DeclaredPlaceholder,
Placeholder,
PlaceholderOptions,
)
register = template.Library()
class RenderGetPlaceholder(Placeholder):
"""
Render the content of a placeholder to a variable. Can be provided
with the name of the placholder (i.e. "Header" in the case of a normal
CMS page) or a template variable containing a placeholder (i.e. myentry.content in the
case of an external app using a placeholder)
{% get_placeholder ["string"|placeholder_var] as variable_name %}
e.g.
{% load extra_cms_tags %}
{% get_placeholder "My Placeholder" as my_placeholder %}
{% if my_placeholder %}
<div>
{{ my_placeholder }}
</div>
{% endif %}
"""
name = "get_placeholder"
options = PlaceholderOptions(
Argument("name", resolve=True),
MultiValueArgument("extra_bits", required=False, resolve=False),
"as",
Argument("varname", resolve=False, required=True),
blocks=[
("endplaceholder", "nodelist"),
],
)
def render_tag(self, context, name, extra_bits, varname, nodelist=None):
if isinstance(name, PlaceholderModel):
content = name.render(context, None)
else:
content = super(RenderGetPlaceholder, self).render_tag(context, name, extra_bits, nodelist)
context[varname] = mark_safe(content)
return ""
def get_declaration(self):
# Fix some template voodoo causing errors
slot = self.kwargs["name"].var.var.__str__().strip('"').strip("'")
return DeclaredPlaceholder(slot=slot, inherit=False)
register.tag(RenderGetPlaceholder)