Как использовать Hogan в шаблоне django

Можно ли включить следующее в мой html-файл django?

<!-- 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, похоже, сначала пытается его проанализировать.


person David542    schedule 17.07.2015    source источник


Ответы (1)


Если вы используете django >= 1.5, попробуйте дословно тег шаблона.

[редактировать]

в более ранних версиях 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))
person Walker    schedule 17.07.2015
comment
Выглядит здорово, но нет, мы используем 1.4. - person David542; 17.07.2015
comment
Ах хорошо. Я обновился с альтернативой. Если вы включите его, вы сможете использовать тег {% verbatim %} так же, как и любой другой тег шаблона. - person Walker; 17.07.2015
comment
Блестящий. Спасибо. - person David542; 17.07.2015