如何告诉Django模板不解析包含看起来像模板标签的代码的块? [英] How can I tell Django templates not to parse a block containing code that looks like template tags?

查看:402
本文介绍了如何告诉Django模板不解析包含看起来像模板标签的代码的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些HTML文件,其中包含 jQuery.tmpl 使用的模板。一些tmpl标签(如 {{if ...}} )看起来像Django模板标签,并导致TemplateSyntaxError。有没有办法我可以指定Django模板系统应该忽略几行,并将它们完全按照原样输出?

I've got some html files that include templates to be used by jQuery.tmpl. Some tmpl tags (like {{if...}}) look like Django template tags and cause a TemplateSyntaxError. Is there a way I can specify the Django template system should ignore a few lines and output them exactly as they are?

推荐答案

内置的方式是使用 templatetag 模板标签手动转义每个模板项目( https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#templatetag ),但我怀疑这不是你想做的。

The built-in way would be to manually escape each template item with the templatetag template tag ( https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#templatetag ), but I suspect that that's not what you want to do.

您真正想要的是将整个块标记为原始(而不是可解释)的文本,这需要一个新的自定义标签。您可能需要在这里查看 raw 标签: http://www.holovaty.com/writing/django-two-phased-rendering/

What you really want is a way to mark a whole block as raw (rather than interpretable) text, which requires a new custom tag. You might want to check out the raw tag here: http://www.holovaty.com/writing/django-two-phased-rendering/

编辑:从Django 1.5开始,这是现在由内置的 逐字记录 模板标签

As of Django 1.5, this is now handled by the built-in verbatim template tag

这篇关于如何告诉Django模板不解析包含看起来像模板标签的代码的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆