Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
chainchan
V2EX  ›  Tornado

模板转义的问题

  •  
  •   chainchan · Dec 10, 2012 · 4751 views
    This topic created in 4902 days ago, the information mentioned may be changed or developed.
    假设当前模板内有如下内容:

    $('#contents').text( " {{ escape(ds['contents']) }} " );


    ds['contents'] = """一


    """

    那么渲染后就变成了

    $('#contents').text( " 一

    三");

    求教一般类似的情况应该如何解决呢?
    3 replies    1970-01-01 08:00:00 +08:00
    reorx
        1
    reorx  
       Dec 10, 2012
    你是不希望它换行吗?
    chainchan
        2
    chainchan  
    OP
       Dec 10, 2012
    @reorx
    应该是说我希望在渲染后的模板中让它显示换行的。

    我尝试自己解决了下:

    逻辑层面把内容切成每一行加上<p>
    ds['contents'] = ''.join(['<p>' + line + '</p>' for line in dic['contents'].split('\n')])

    视图层
    $('#contents').html( " {% raw ds['contents'] %} " );
    reorx
        3
    reorx  
       Dec 10, 2012
    我觉得还是把和 html 有关的东西都放在模板里面比较好呢。

    ds['contents'] = ['一', '二', '三']

    # template
    var contents = $('#contents');
    {% for line in ds['contents'] %}
    $('<p>{{ line }}</p>').appendTo(contents);
    {% end %}
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3610 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:38 · PVG 18:38 · LAX 03:38 · JFK 06:38
    ♥ Do have faith in what you're doing.