2012-04-24 137 views
0

我写了一个自定义模板标签,它返回聊天中的许多用户。django模板标签中的变量值

{% chat_online chat_channel %}

然而,令牌似乎无法接收该值作为chat_channel的而不是变量的值。

怎么回事?

+0

除非您向我们展示代码,否则我们如何才能告诉您自定义标记有什么问题? – 2012-04-24 12:08:59

回答

2

请记住,HTML中的模板标记定义({% ... %})只是由django的模板引擎解析的文本文件,因此您需要tell django to actually lookup the variable in the context being rendered with the name chat_channel。从文档这个例子是很清楚的:

class FormatTimeNode(template.Node): 
    def __init__(self, date_to_be_formatted, format_string): 
     self.date_to_be_formatted = template.Variable(date_to_be_formatted) 
     ... 

    def render(self, context): 
     try: 
      actual_date = self.date_to_be_formatted.resolve(context) 
      ... 
     except template.VariableDoesNotExist: 
      return '' 

其中template.Variable(date_to_be_formatted)创造从(在本例中blog_entry.date_updated)传递给模板标签的原始值模板变量和self.date_to_be_formatted.resolve(context)是找到变量的实际值通过针对上下文解决模板。