【问题标题】:Parse Custom html in Template Tags, django 1.4在模板标签中解析自定义 html,django 1.4
【发布时间】:2012-04-30 05:55:00
【问题描述】:

如何在自定义标记类 (template.Node) 中使解析器呈现带有标记的 html 的 sn-p? 例如:

@register.tag(name='addspam')
class AddSpam(template.Node):
    def __init__(self, parser, token): ...
    def render(self, context):
        spam_html = "SPAM {{ any_tag_here }} SPAM"
        return spam_html

这里,AddSpam,当“调用”时,返回“SPAM {{ any_tag_here }} SPAM”,而不呈现 any_tag_here。这显然是可以预测的,但是我怎样才能更改返回值,以便将 any_tag_here 呈现为好像它是“本机”的?有什么方法可以使用我可以使用的上下文吗?

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:
    def render(self, context):
        spam_html = "SPAM %(any_tag_here)s SPAM" % context
        return spam_html
    

    【讨论】:

      【解决方案2】:

      我无法解决问题。我选择了另一种方法:我现在正在做的不是在 html 中呈现标签

      {% addspam %}
          {{ any_tag_here }}
      {% end_addpsam %}
      

      这有助于我的代码松散耦合,并且在我遇到的特定情况下工作得很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-20
        • 2021-07-13
        • 2015-07-25
        • 2012-03-15
        • 2013-02-01
        • 2018-06-17
        • 1970-01-01
        相关资源
        最近更新 更多