Django 中自定義 tag 如何獲取變量參數的值

這兩天學着寫了個 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)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章