douba7784 2012-08-09 07:35
浏览 98
已采纳

使用TWIG模板渲染干净整洁的HTML

I've been playing with Twig (version 1.9.1) as a PHP templating engine, and I'm not totally satisfied with the rendered HTML as extra unwanted line-breaks and spaces are added which make the result quite untidy. To make my templates more flexible, I am using macros which can call each other in a nested fashion, which in itself works fine, but seems to make things worse. For instance, a <a> element is rendered as follows:

             <a href="http://google.com" alt="some alternative text">                    some text with <strong>some html</strong>



</a>

I know that part of the reason is the way my twig templates are formatted, as if I remove empty lines and indenting from them, the rendered HTML looks a bit more tidy, but not completely: the below output shows a rendered <a> with still 2 line breaks between the start and end of the element, despite the corresponding template not containing any empty lines or spaces!:

<a href="http://google.com" alt="some alternative text">some text with <strong>some html</strong>


</a>

Even though it helps, removing formatting from my twig templates (i.e. empty lines and indenting) isn't really an option as it makes my template very hard to read and maintain.

Apart from removing formatting from templates. what are the ways to render a cleaner/tidier HTML with Twig?

  • 写回答

3条回答 默认 最新

  • doude5860 2012-08-09 13:53
    关注

    This page of the documentation should help you control spaces within tags http://twig.sensiolabs.org/doc/templates.html#whitespace-control

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器