您的位置:首页 > 编程语言 > Go语言

Django 中自定义 tag 如何获取变量参数的值

2007-03-02 02:18 435 查看
这两天学着写了个 Django 的 tag,这个 tag 叫做 "post_detail_tag", 调用的代码像这样:

{% post_detail_tag post.id current_post_id %}
其中后面两个都是传递的变量。

结果我发现在 tag 的代码中,一开始解析传递过去的 token 时,能获取到的仅仅是 "post.id", "current_post_id" 这种变量名,而无从获取其变量值。

当然了,仔细想想的确也应该这样,因为这个时候还没有 context. 传进 tag 的仅仅是 "post_detail_tag post.id current_post_id" 这个字符串罢了,还在 tokenize 的低级阶段。

因此自然想到,实际的变量值获取必须在 tag 对应的 Node 类的 render 方法中才可以获取到,因为这个时候参数中会传进来 context.

奇怪的是获取变量值这样一个常见的需求却没有在文档中看到说明,相关范例代码里也找不到。也许是我看的不仔细吧。。

翻看 D:/Python24/Lib/site-packages/Django-0.95.1-py2.4.egg/django/template/__init__.py 的源代码才找到 resolve_variable 这个函数。另外对于 FilterExpression 而言也有 resolve 函数,但这个内部还是调用 resolve_variable 的。因此导入这个函数试了一下,发现可以取到变量的值了。

代码:
from django import template
from django.template import Context, Template, loader, resolve_variable

register = template.Library()

class PostDetailNode(template.Node):
    def __init__(self, id, current_post_id):
        self.id = id
        self.current_post_id = current_post_id        
        pass
    
    def render(self, context):       
        current_post_id = int(resolve_variable(self.current_post_id, context))        
        context['current_post_id'] = current_post_id
        t = loader.get_template("forum/templatetags/post_detail.html")        
        return t.render(context)

#@register.tag(name='post_detail_tag')
def do_post_detail_tag(parser, token):
    try:
        #tag_name, args = token.contents.split(None, 1)
        #id, current_post_id = args.split(None, 1)
        tag_name, id, current_post_id = token.split_contents()
        print id, current_post_id
    except ValueError:        
        raise template.TemplateSyntaxError, "%s tag requires argument" % tag_name
    
    return PostDetailNode(id, current_post_id)

register.tag('post_detail_tag', do_post_detail_tag)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django tokenize token 文档
相关文章推荐