如何防止Django解释包含卷曲引号的块? [英] How do I prevent Django from interpreting a block which contains curly quotes?

查看:142
本文介绍了如何防止Django解释包含卷曲引号的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个使用Django模板的网站,而且我必须生成包含配对的卷曲引号的HTML。有没有办法禁用Django的代码块的标签处理?像一个文字块标签会很好,所以:

I'm working on a website which uses Django templates, and I have to produce HTML which contains paired curly quotes. Is there any way to disable Django's tag processing for a block of code? Something like a literal block tag would be nice, so that:

{% literal %}
 {% LITERAL {{ BRACES }}
{% endliteral %}

...会产生 {%LITERAL {{BRACES}} 在输出中。

... would produce {% LITERAL {{ BRACES }} in the output.

推荐答案

编辑:
您的语法目前不可能使用当前的词法分析器/解析器系统。

Your syntax is currently impossible with the current lexer / parser system.

为什么?基本上模板系统有一个Lexer和一个解析器。 Lexer将模板字符串作为输入,并将其标记。解析器然后在其构造函数中获取令牌列表,并将其解析为编译模板的一堆节点的列表。模板标记和过滤器只能访问已构建的解析器 - 您无法访问初始词法分析器字符串。请参阅 django / templates / __ init __。py

Why? Basically the template system has a Lexer and a Parser. The Lexer takes the template string as input, and tokenizes it. The parser then takes the list of tokens in its constructor and parses them into a list of a bunch of Nodes for the compiled template. The template tags and filters only have access to the already constructed parser -- you can't access the initial lexer string. See the comments in django/templates/__init__.py

中的注释。但是,有一个解决方案。这不是我的(见下文),但它基本上使用服务器端包括{%ssi some_file.html%}来包含一个具有文字文字的额外文件。是的,这是一个丑陋的解决方案但是没有大量重写模板系统,它将足够了。

However, there is a solution. It's not mine (see below), but its to basically use server side includes {% ssi some_file.html %} to include an extra file that has the literal text. Yes this is an ugly solution; but without a major rewrite of the templating system it will have to suffice.

简单的方法来逃避Django模板变量

这篇关于如何防止Django解释包含卷曲引号的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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