简体   繁体   English

如何从django-cms自定义templatetag渲染占位符?

[英]How to render placeholders from a django-cms custom templatetag?

I've made a simple template tag to list child pages in a django-cms site. 我已经做了一个简单的模板标签来列出django-cms网站中的子页面。 It's working fine except for the fact that I have not been able to render the child pages placeholders in the template tag itself. 除我无法在模板标记本身中呈现子页面占位符的事实外,它工作正常。

The following is my code for the template tag. 以下是我的模板标记代码。

subpages.py subpages.py

from cms.models import Page
from cms.utils.page_resolver import get_page_from_path

from django import template


register = template.Library()


@register.inclusion_tag('subpages.html', takes_context = True)
def get_news_items( context ):
    request = context['request']
    subpages = request.current_page.children.filter(published=True)
    return {'subpages':subpages}

subpages.html subpages.html

{% load cms_tags menu_tags placeholder_tags %}
<ul>
{% for item in subpages %}
    <li><a href="/{{ item.get_path }}">{{ item.get_title }}</a>
        {% render_placeholder subtitle %}
    </li>
{% endfor %}
</ul>

I've tried some alternatives to *render_placeholder* but without luck. 我尝试了一些替代* render_placeholder *的方法,但是没有运气。

How would be the correct way to get the placeholder rendered? 如何获得占位符的正确方法?

This is only a (untested) suggestion, but try passing the context along to the template: 这只是(未试用的)建议,但请尝试将上下文传递给模板:

@register.inclusion_tag('subpages.html', takes_context = True)
def get_news_items( context ):
    request = context['request']
    subpages = request.current_page.children.filter(published=True)
    context.update({'subpages':subpages})
    return context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM