如何在django模板中使用Hogan

How to use Hogan in django template

是否可以在我的 django html 文件中包含以下内容?

<!-- Hit template -->
<script type="text/template" id="hit-template">
  <div class="hit media">
    <a class="pull-left" href="{{ url }}">
      <img class="media-object" src="{{ image }}" alt="{{ name }}">
    </a>
    <div class="media-body">
      <h3 class="hit_price pull-right text-right text-danger">
        ${{ salePrice }}
      </h3>
      <h4 class="hit_name">{{{ _highlightResult.name.value }}}</h4>
      <p>
        {{{ _highlightResult.shortDescription.value }}}
      </p>
      <ul class="hit_tags list-inline">
        {{#_highlightResult.manufacturer}}<li>{{{ _highlightResult.manufacturer.value }}}</li>{{/_highlightResult.manufacturer}}
        {{#_highlightResult.category}}<li>{{{ _highlightResult.category.value }}}</li>{{/_highlightResult.category}}
        {{#type}}<li>{{{ type }}}</li>{{/type}}
      </ul>
    </div>
  </div>
</script>

当我当前包含它时,我收到一个 Django 错误,因为 Django 模板引擎似乎首先尝试解析它。

如果你是 运行 django >= 1.5,试试 verbatim 模板标签。

[编辑]

在早期版本的 django 上,您应该能够使用以下内容自己复制模板标签功能:

"""
From https://gist.github.com/1313862
"""

from django import template

register = template.Library()


class VerbatimNode(template.Node):

    def __init__(self, text):
        self.text = text

    def render(self, context):
        return self.text


@register.tag
def verbatim(parser, token):
    text = []
    while 1:
        token = parser.tokens.pop(0)
        if token.contents == 'endverbatim':
            break
        if token.token_type == template.TOKEN_VAR:
            text.append('{{')
        elif token.token_type == template.TOKEN_BLOCK:
            text.append('{%')
        text.append(token.contents)
        if token.token_type == template.TOKEN_VAR:
            text.append('}}')
        elif token.token_type == template.TOKEN_BLOCK:
            text.append('%}')
    return VerbatimNode(''.join(text))