javascript - 如何在 django 模板中使用 Hogan

标签 javascript python django hogan.js

是否可以在我的 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))

关于javascript - 如何在 django 模板中使用 Hogan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31466705/

相关文章:

javascript - addClass 不会在多个元素上同时发生

python - 在 CMake 中运行调用 Glad

python - 保存时格式化会删除 VS-Code 中的导入语句

带有 self 实例的 Django 模型方法

python - 部署在pythonanywhere上的django项目链接静态文件出错

javascript - 隐藏滚动条 iFrame 子网站

javascript - 什么类型的对象是 Javascript 原型(prototype)?

javascript - 检查字段是否包含数据并且其中一个是数字

python - 如何从python中的s字符串中删除前4个字节

python - django 不正确地将日期时间输入数据库