Skip to content

Commit

Permalink
Add render_object_placeholder template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 6, 2023
1 parent 5499a61 commit 9eadb0f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
26 changes: 15 additions & 11 deletions djangocms_blog/templates/djangocms_blog/includes/blog_item.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
{% load djangocms_blog i18n easy_thumbnails_tags cms_tags %}

<article id="post-{{ postcontent.slug }}" class="post-item">
<article id="post-{{ post_content.slug }}" class="post-item">
<header>
<h3><a href="{% absolute_url postcontent %}">{{ postcontent.title }}</a></h3>
{% if postcontent.subtitle %}
<h4>{{ postcontent.subtitle }}</h4>
<h3><a href="{% absolute_url post_content %}">{{ post_content.title }}</a></h3>
{% if post_content.subtitle %}
<h4>{{ post_content.subtitle }}</h4>
{% endif %}
{% block blog_meta %}
{% include "djangocms_blog/includes/blog_meta.html" %}
{% endblock %}
</header>
{% if image and postcontent.post.main_image %}
{% if image and post_content.post.main_image %}
<div class="blog-visual">
{% thumbnail postcontent.post.main_image postcontent.post.thumbnail_options.size crop=postcontent.post.thumbnail_options.crop upscale=postcontent.post.thumbnail_options.upscale subject_location=postcontent.post.main_image.subject_location as thumb %}
<img src="{{ thumb.url }}" alt="{{ postcontent.main_image.default_alt_text }}" width="{{ thumb.width }}" height="{{ thumb.height }}" />
{% thumbnail post_content.post.main_image post_content.post.thumbnail_options.size crop=post_content.post.thumbnail_options.crop upscale=post_content.post.thumbnail_options.upscale subject_location=post_content.post.main_image.subject_location as main_image %}
{% if main_image %}
<img src="{{ main_image.url }}" alt="{{ post_content.main_image.default_alt_text|default:'' }}" width="{{ main_image.width }}" height="{{ main_image.height }}" />
{% else %}
<img src="{{ post_content.post.main_image.url }}" alt="{{ post_content.main_image.default_alt_text|default:'' }}" />
{% endif %}
</div>
{% else %}
{% media_images postcontent as previews %}
{% media_images post_content as previews %}
<div class="blog-visual">
{% for preview in previews %}<img src="{{ preview }}" />{% endfor %}
</div>
{% endif %}
<div class="blog-lead">
{% if not TRUNCWORDS_COUNT %}
{% render_model postcontent "abstract" "" "" "safe" %}
{% render_model post_content "abstract" "" "" "safe" %}
{% else %}
{% render_model postcontent "abstract" "" "" "truncatewords_html:TRUNCWORDS_COUNT|safe" %}
{% render_model post_content "abstract" "" "" "truncatewords_html:TRUNCWORDS_COUNT|safe" %}
{% endif %}
</div>
<footer class="read-more">
<a href="{% absolute_url postcontent %}">{% trans "read more" %} &raquo;</a>
<a href="{% absolute_url post_content %}">{% trans "read more" %} &raquo;</a>
</footer>
</article>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% load i18n %}{% spaceless %}
<div class="plugin plugin-blog">
<div class="blog-latest-entries">
{% for postcontent in postcontent_list %}
{% include "djangocms_blog/includes/blog_item.html" with postcontent=postcontent image="true" TRUNCWORDS_COUNT=TRUNCWORDS_COUNT %}
{% for post_content in postcontent_list %}
{% include "djangocms_blog/includes/blog_item.html" with post_content=post_content image="true" TRUNCWORDS_COUNT=TRUNCWORDS_COUNT %}
{% empty %}
<p class="blog-empty">{% trans "No article found." %}</p>
{% endfor %}
Expand Down
18 changes: 9 additions & 9 deletions djangocms_blog/templates/djangocms_blog/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
{% load i18n easy_thumbnails_tags cms_tags %}

{% block canonical_url %}<link rel="canonical" href="{{ meta.url }}"/>{% endblock canonical_url %}
{% block title %}{{ post.get_title }}{% endblock %}
{% block title %}{{ post_content.title }}{% endblock %}

{% block content_blog %}{% spaceless %}
<article id="post-{{ post_content.slug }}" class="post-item post-detail">
<header>
<h2>{{ post_content.title }}{# render_model post "title" #}</h2>
<h2>{% render_model post_content "title" %}</h2>
{% if post_content.subtitle %}
<h3>{# render_model post "subtitle" #}</h3>
<h3>{% render_model post_content "subtitle" %}</h3>
{% endif %}
{% block blog_meta %}
{% include "djangocms_blog/includes/blog_meta.html" %}
Expand All @@ -19,20 +19,20 @@ <h3>{# render_model post "subtitle" #}</h3>
<div class="blog-visual">{% render_placeholder post_content.media %}</div>
{% else %}
<div class="blog-visual">
{% thumbnail post_content.post.main_image post_content.post.full_image_options.size crop=post_content.post.full_image_options.crop upscale=post_content.post.full_image_options.upscale subject_location=post_content.post.main_image.subject_location as thumb %}
<img src="{{ thumb.url }}" alt="{{ post_content.post.main_image.default_alt_text }}" width="{{ thumb.width }}" height="{{ thumb.height }}" />
{% thumbnail post_content.post.main_image post_content.post.full_image_options.size crop=post_content.post.full_image_options.crop upscale=post_content.post.full_image_options.upscale subject_location=post_content.post.main_image.subject_location as main_image %}
<img src="{{ main_image.url }}" alt="{{ post_content.post.main_image.default_alt_text }}" width="{{ main_image.width }}" height="{{ main_image.height }}" />
</div>
{% endif %}
{% endspaceless %}
{% if post_content.post.app_config.use_placeholder %}
<div class="blog-content">{% render_placeholder post.content %}</div>
<div class="blog-content">{% render_placeholder post_content.content %}</div>
{% else %}
<div class="blog-content">{% render_model post_content "post_text" "post_text" "" "safe" %}</div>
{% endif %}
{% if post.related.exists %}
{% if post_content.related.exists %}
<section class="post-detail-list">
{% for related in post.related.all %}
{% include "djangocms_blog/includes/blog_item.html" with post=related image="true" TRUNCWORDS_COUNT=TRUNCWORDS_COUNT %}
{% for related in post_content.related.all %}
{% include "djangocms_blog/includes/blog_item.html" with post_content=related image="true" TRUNCWORDS_COUNT=TRUNCWORDS_COUNT %}
{% endfor %}
</section>
{% endif %}
Expand Down
48 changes: 48 additions & 0 deletions djangocms_blog/templatetags/djangocms_blog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from classytags.arguments import Argument
from classytags.core import Options
from classytags.helpers import AsTag
from cms.toolbar.utils import get_toolbar_from_request
from cms.utils.placeholder import get_placeholder_from_slot
from cms.utils.plugins import get_plugins
from cms.utils.urlutils import admin_reverse
from django import template
Expand Down Expand Up @@ -121,3 +123,49 @@ def endpoint_url(admin, obj):


register.tag(GetAbsoluteUrl.name, GetAbsoluteUrl)


class RenderNamedPlaceholder(AsTag):
"""
Render the content of the plugins contained in a placeholder.
The result can be assigned to a variable within the template's context by using the `as` keyword.
It behaves in the same way as the `PageAttribute` class, check its docstring for more details.
"""

name = "render_object_placeholder"
options = Options(
Argument("placeholder"),
Argument("object"),
"language",
Argument("language", default=None, required=False),
"as",
Argument("varname", required=False, resolve=False),
)

def _get_value(self, context, editable=True, **kwargs):
request = context["request"]
toolbar = get_toolbar_from_request(request)
renderer = toolbar.get_content_renderer()
placeholder = kwargs.get("placeholder")
obj = kwargs.get("object")
nocache = kwargs.get("nocache", False)

if not placeholder or not isinstance(placeholder, str) or not hasattr(obj, "placeholders"):
return ""

placeholder = get_placeholder_from_slot(obj.placeholders, placeholder)
content = renderer.render_placeholder(
placeholder=placeholder,
context=context,
language=kwargs.get("language"),
editable=editable,
use_cache=not nocache,
width=kwargs.get("width"),
)
return content

def get_value_for_context(self, context, **kwargs):
return self._get_value(context, editable=False, **kwargs)

def get_value(self, context, **kwargs):
return self._get_value(context, **kwargs)

0 comments on commit 9eadb0f

Please sign in to comment.