【问题标题】:How to prevent space in output due to line break in the HTML code?如何防止由于 HTML 代码中的换行导致输出中的空格?
【发布时间】:2013-04-30 11:29:51
【问题描述】:

我在这里看到了这个问题,有些人正在通过评论标签或破坏标签来解决这个问题:

 <i><!--
-->t does not work on the first level with tabspace = 2
   beside it looks horrible.  
<a
 >nnoying<
/a> is it not?

我记得好像我见过一些类似的把戏

<a>&dont_put_here_space_please;
  tricky ampersand code.

有这样的事吗?

我现在正在使用 jinja2 python 模板引擎。它有一些防止空间的技巧吗?

更新:

借助 jinja2,感谢 dav1d,我能想到的最短方法是:

<a>{% if true -%}
  No space in the output before this text.
</a>{% endif -%}
  Nor after.

有没有更短的方法?

【问题讨论】:

  • 你在想   ?
  • @remyabel: 正好相反 :) 类似 &nosp;
  • 你见过this
  • @dav1d 哦,酷!您能否将其放入答案中以便我接受?
  • 好的,添加为答案

标签: html jinja2


【解决方案1】:

我知道这是一个老问题,但我想分享一下我是如何在我的代码中解决这个问题的:使用 span。

<span> .../line1... /line2 ... </span>

【讨论】:

    【解决方案2】:

    这应该做你想做的:

    {# -#}
    

    【讨论】:

    • 这个是最短的。我想这比玩if true 更好。
    【解决方案3】:

    Jinja2 WhiteSpace Control 是您所需要的(根据您对我的评论的回复):http://jinja.pocoo.org/docs/templates/#whitespace-control

    【讨论】:

    • 还没有...你能告诉我你会怎么做吗?我试过&lt;a&gt;{% -%},但它说这是不允许的。 :(
    • 呃,抱歉帮不了你,我自己用 Jinja 有一段时间了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多