如何获取变量中的html标签以起作用? [英] How do I get html tags in variables to work?

查看:108
本文介绍了如何获取变量中的html标签以起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下

Python:

render = web.template.render('templates/', base="layout")
.
.
.
fileout_text = codecs.open(filename_text, 'r', 'utf-8').read()
text = markdown.markdown(fileout_text)
return render.text_temple(text=text)

text_template.html:

$def with text
     < text>$text < /text>

现在$text应该包含html标签,而不是markdown语法.

Now $text should contain html tags instead of markdown syntax.

我的问题是,在显示网站时,标记会保留为文本–为什么呢?

My problem is that the tags stay as text when the website is displayed – why is that?

推荐答案

您的模板系统会将HTML转义为安全措施.您需要告诉模板Markdown的HTML输出是安全的".根据您的代码,我假设您正在使用web.py. web.py 文档状态:

Your template system is escaping the HTML as a security measure. You need to tell the template that the HTML output by Markdown is "safe". Based on your code, I'm assuming you are using web.py. The web.py docs state:

默认情况下,Templetor使用web.websafe过滤器进行HTML编码.

By default, Templetor uses web.websafe filter to do HTML-encoding.

>>> render.hello("1 < 2")
"Hello 1 &lt; 2"

要关闭过滤器,请在$之后使用:.例如:

To turnoff filter use : after $. For example:

The following will not be html escaped.
$:form.render()

因此,请在您的模板中尝试以下操作:

So in your template try this:

$def with text
     < text>$:text < /text>

添加冒号($:text)将告诉模板系统不要转义HTML.

Adding the colon ($:text) will tell the template system to not escape the HTML.

如果我猜错了并且您没有使用web.py,请告诉我们您使用的是哪个模板系统,也许我们可以为您提供正确的解决方案.尽管现在您知道了问题所在,但是您可以只搜索该模板系统的文档,然后自己找到答案.

If I guessed incorrectly and you are not using web.py, then tell us which templating system you are using and perhaps we can point you to the right solution. Although, now that you know what the problem is, you could just search the docs for that templating system and find the answer yourself.

这篇关于如何获取变量中的html标签以起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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