简单的方法来逃避Django模板变量 [英] Easy Way to Escape Django Template Variables
问题描述
对于一个新项目,我们正在编写关于 Django模板系统的文档。我们也使用Django作为文档项目本身,所以Django会在示例代码中提取所有的示例变量,并尝试渲染它们。我们发现,唯一的办法是使用 {%templatetag% } ,但这使得我们的代码真的不可读。是否有可能让Django忽略特定部分的所有模板变量?
For a new project we're writing documentation about the Django template system. We use Django for the documentation project itself too, so Django picks up all our example variables in the sample code and tries to render them. The only way we found to get around this is to use {% templatetag %}, but that makes our code really unreadable. Is there maybe a way to make Django ignore all template variables in a specific section?
推荐答案
由于Django模板词法分析器中的限制(就像是一个kludgy黑客),这是不可能的。但是,如果您愿意将示例代码放在单独的文件中,可以使用 ssi
标签:
Due to limitations in the Django template lexer (like being a kludgy hack), this is impossible. However, if you are willing to put your example code in separate files, you can use the ssi
tag:
{% ssi /path/to/my/code/examples/example01.html %}
它不会解析文件,只是包含它逐字。然而,这也有限制,因为您不能在包含路径中使用变量(即,如果您移动模板位置,则必须重写或至少找到并替换您的模板文件),并且必须将包含)中的
。 (请参阅 http://docs.djangoproject.com/en/dev/ ref / templates / builtins /#ssi 。) ALLOWED_INCLUDE_ROOTS
设置中的路径(即 / path / to / my / code / examples
) C> settings.py
And it won't parse the file, just include it verbatim. However, this also has limitations in that you can't use variables in the include path (i.e. if you move template locations, you have to rewrite or at least find-and-replace your template files), and you have to put the include path (i.e. /path/to/my/code/examples
) in the ALLOWED_INCLUDE_ROOTS
setting in your settings.py
. (See http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ssi.)
这篇关于简单的方法来逃避Django模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!