Python 金字塔 & Chameleon 模板语言转义 html
我无法理解变色龙的标签。我是 django 用户,但决定向我的 CompSci 课程伙伴和我自己介绍 Pyramid,因为我认为 Pyramid 更轻量 = 更容易学习。
目前, ${} 标签正在转义我试图通过它输出的任何 html 标签。在 django 中,有某种方法可以指定变量是“安全的”并且不需要转义。
我怎样才能在金字塔/变色龙中做同样的事情?
I can't make sense of chameleon's tags. I'm a django user, but decided to introduce my CompSci course mates and myself to Pyramid, since I though more lightweight = easier to learn.
At the moment the ${} tag is escaping any html tags I'm trying to output through it. In django there was some way to specify that a variable is "safe" and doesn't need to be escaped.
How can I do the same thing in Pyramid / Chameleon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Chameleon 还允许 ${struct: markup}。
Chameleon also allows ${structure: markup}.
Chameleon 基于 Zope 页面模板 库,因此如果您发现缺少 Chameleon 文档,您可能希望查看 zpt 文档。
无论如何,有两种主要方法可以做到这一点。如果您使用 tal:replace 或 tal:content 标签属性进行渲染,则可以使用
如果您不想使用 tal:replace 或 tal:content 函数,则需要将字符串包装在 Chameleon 渲染器不会尝试转义的对象中(这意味着它有一个
__html__
方法返回字符串应该是什么)。通常,这意味着创建一个“Literal”类,如下所示:Chameleon is based on the Zope Page Templates library, so if you find the Chameleon documentation lacking, you might wish to check out the zpt docs.
In any case, there are two main ways to do this. If you are rendering using a tal:replace or tal:content tag attribute, you can use a "structure". This is done by putting
structure
at the beginning of the string, followed by a space, and finally the name of the template variable you wish to render. An example is shown below:If you don't want to use the tal:replace or tal:content functions, you need to wrap your string in an object that the Chameleon renderer will not try to escape (meaning it has an
__html__
method that returns what the string should be). Typically, this means creating a 'Literal' class as shown below: