变色龙的换行 [英] Line Breaking in Chameleon

查看:114
本文介绍了变色龙的换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了金字塔框架来构建大型Web应用程序.

I have used the pyramid framework to build a large web application.

除其他事项外,该应用程序还允许用户在文本区域表单字段中输入文本.然后将此文本保存到数据库中,当然可以再次读出并在以后显示.

Among other things, this application allows the user to enter text into a text area form field. This text is then saved to a database and of course can be readout again and displayed later.

要显示内容,我正在使用 Chameleon模板引擎.

To display content I am using the Chameleon Template Engine.

这很好用,只是换行符没有正确显示(根本不显示).这可能是由于以下事实:在通过Chameleon显示时,输入到文本区域的换行符不会在HTML中引起换行符.如何解决这个问题?

This works fine, except that line breaking is not displayed correctly (not displayed at all). This is probably due to the fact that the newlines entered into the text area do not cause a new line in HTML when displayed through Chameleon. How can one fix this?

<br> -Tags替换换行符是没有帮助的,因为默认情况下Chameleon会转义所有HTML-Tags.我知道有人可以停用此功能,但是我不想这样做,以避免跨站点脚本编写.

It does not help to replace the newlines by <br>-Tags because by default Chameleon escapes all HTML-Tags. I am aware of the fact that one can deactivate this feature, but I do not want to do that to avoid cross-site scripting.

推荐答案

您需要将文本分成几行,然后使用循环和<br/>标签进行渲染:

You need to break the text into separate lines, then render this using a loop and <br/> tags:

<span tal:omit-tag="" 
      tal:repeat="line text_with_newlines.splitlines()">
  ${line}<br />
</span>

这使用 str.splitlines()方法来拆分在换行符上输入文本,然后循环在该文本的每一行之后添加一个<br />中断标签.

您很正确,不要在视图中执行此操作,然后通过设置structure:标志来强制Chameleon接受插入的<br />标签.幸运的是,绝对没有必要.

You are quite right not doing this in the view, then forcing Chameleon to accept your inserted <br /> tags by setting the structure: flag. Luckily there is absolutely no need for that anyway.

这篇关于变色龙的换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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