开发者

Django simple_tag and setting context variables

开发者 https://www.devze.com 2023-02-09 12:17 出处:网络
Im trying to use a simple_tag and set a context variable. i am using the trunk version of django from django import template

Im trying to use a simple_tag and set a context variable. i am using the trunk version of django

from django import template

@register.simple_tag(takes_context=True)
def somefunction(context, obj):   
    return set_context_vars(obj)

class set_context_vars(template.Node):
    de开发者_JS百科f __init__(self, obj):
        self.object = obj

    def render(self, context):
        context['var'] = 'somevar'
        return ''

This doesnt set the variable, but if I do something very similar with @register.tag it works but the object parameter doesn't pass through...

Thanks!


You are mixing two approaches here. A simple_tag is merely a helper function, which cuts down on some boilerplate code and is supposed to return a string. To set context variables, you need (at least with plain django) to write your own tag with a render method.

from django import template

register = template.Library()


class FooNode(template.Node):

    def __init__(self, obj):
        # saves the passed obj parameter for later use
        # this is a template.Variable, because that way it can be resolved
        # against the current context in the render method
        self.object = template.Variable(obj)

    def render(self, context):
        # resolve allows the obj to be a variable name, otherwise everything
        # is a string
        obj = self.object.resolve(context)
        # obj now is the object you passed the tag

        context['var'] = 'somevar'
        return ''


@register.tag
def do_foo(parser, token):
    # token is the string extracted from the template, e.g. "do_foo my_object"
    # it will be splitted, and the second argument will be passed to a new
    # constructed FooNode
    try:
        tag_name, obj = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires exactly one argument" % token.contents.split()[0]
    return FooNode(obj)

This may be called like this:

{% do_foo my_object %}
{% do_foo 25 %}


Since Django 1.9, it is possible to store simple_tag results in a template variable by using the as argument followed by the variable name:

@register.simple_tag
def current_time(format_string):
    return datetime.datetime.now().strftime(format_string)
{% current_time "%Y-%m-%d %I:%M %p" as the_time %}
<p>The time is {{ the_time }}.</p>
0

精彩评论

暂无评论...
验证码 换一张
取 消