2012-04-24 134 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)是找到變量的實際值通過針對上下文解決模板。