gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:05 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com