如何将参数传递给django的自定义模板(块)标签

时间:2014-05-26 05:41:56

标签: python django django-templates

我试图用django制作可重用的界面组件。我能够使用自定义模板标记来解决问题:

{% panel %}
    Some panel content
{% endpanel %}

它工作正常,但我无法将面板标题作为参数传递。我真正想要的是:

{% panel 'Panel Title' %}
    Some panel content
{% endpanel %}

我无法在文档中找到答案。有没有办法实现它?如果没有,我可以使用另一种策略吗?

  

编辑:请注意,面板内容可以在内部接受更多标记,就像其他标记和块一样。这就是为什么一个简单的模板标签对我来说还不够。


详细

panel.html

<div class="panel panel-default">
    <div class="panel-heading">{{ title|default:"Panel Title" }}</div>
    <div class="panel-body">
        {{ body }}
    </div>
</div>

templatetags / theme.py

from django import template


register = template.Library()


@register.tag
def panel(parser, token):
    nodelist = parser.parse(('endpanel',))
    parser.delete_first_token()
    return PanelNode(nodelist)


class PanelNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        output = self.nodelist.render(context)
        t = template.loader.get_template('theme/blocks/panel.html')
        c = template.Context({'body': output})
        return t.render(c)

2 个答案:

答案 0 :(得分:1)

我相信你要找的答案写在文档中, https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#passing-template-variables-to-the-tag

转到简单标签的部分,它详细说明了如何以您想要的方式传递可用性

@register.simple_tag
def my_tag(a, b, *args, **kwargs):
    warning = kwargs['warning']
    profile = kwargs['profile']
    ...
    return ...

然后在模板中,任何数量的参数(以空格分隔)都可以传递给模板标记。与Python类似,关键字参数的值使用等号(“=”)设置,并且必须在位置参数之后提供。例如:

{% my_tag 123 "abcd" book.title warning=message|lower profile=user.profile %}

答案 1 :(得分:0)

我遇到了同样的问题,我在查看你的问题之后设法做到了。希望这可以帮助其他人。

我在我的项目中使用classy-tags,我强烈推荐它。如果您计划仅使用一次,则代码可能很简单。由于我的意图是使用不同类型的面板,我已经使它有些通用了。

# templatetags/modals.py
from classytags.core import Tag, Options
from classytags.arguments import Argument, MultiKeywordArgument
from django import template

register = template.Library()


class BasePanel(Tag):

    def render_tag(self, context, **kwargs):

        nodelist = kwargs.pop('nodelist')
        body = nodelist.render(context)

        panel_context = self.get_panel_context(kwargs)
        panel_context['body'] = body

        t = template.loader.get_template(self.template)
        c = template.Context(kwargs)

        return t.render(c)

    def get_panel_context(self, arguments):
        """
            Override this method if you're using fancy arguments,
            e.g: MultiKeywordArgument
        """
        return arguments


class ExamplePanel(BasePanel):
    name = 'panel'
    template = 'main/modals/panel.html'
    options = Options(
        Argument('title'),
        MultiKeywordArgument('kw', required=False),
        blocks=[('endpanel', 'nodelist')],
    )

    def get_panel_context(self, arguments):
        kw = arguments.pop('kw')
        arguments['state'] = kw.get('state', 'default')
        return arguments

register.tag(ExamplePanel)

这是模板:

# panel.html
<div class="panel panel-{{ state }}">
  <div class="panel-heading">
    <h3 class="panel-title">{{ title }}</h3>
  </div>
  <div class="panel-body">
    {{ body }}
  </div>
</div>

以下是如何使用它:

{% panel "First Title" state="primary" %}

<p>Hello World!</p>

{% endpanel %}

{% panel "Another Title" %}

<p>Hello World!</p>

{% endpanel %}

使用Jinja2 Macros可以轻松完成此操作。希望Django 1.8能够带来原生的Jinja2支持。