douxianwu2221 2014-02-20 07:55
浏览 90
已采纳

Twig,输出转义和语法高亮JS插件

I'm struggling with following issue: I want to include some code examples on my page. They are mostly php, but also html and js.

My best option is to use some of js-based syntax highlighters. I choose SyntaxHighlighter, because so many people recommend this on SO and other sites.

But what about output escaping in twig? Of course default escaping is causing code to show beatifully escaped, but this doesn't work properly. Using |raw to make this work results in other - obvious - issue: it breaks page if html is presented, or breaks scripts if javascript is presented.

Another issue is that all the outputs that I need escaped are mixed, eg:

(some text, with html formatting)

<code class="someclass">
  (block of code)
</code>

(some another text)

<code class="anotherclass">
  (another block of code)
</code>

Having all these fact I thought - let's write our own filter for Twig! That sounds great, but even if I was able to make it run, I coudn't make it work the way I want.

They I thought - why should I reinvent the wheel? Twig and Symfony2 are out for years, probably someone else already solved this problem, and did it good, secure way.

I'm looking for one of four things:

  1. Custom twig filter to handle this issue, or
  2. Better syntax coloring script, that will handle output escaped by Twig, or
  3. Some other solution, or
  4. Any useful hint.
  • 写回答

1条回答 默认 最新

  • douse8732 2014-02-20 09:47
    关注

    Have a look at this manual:

    http://isometriks.com/geshi-symfony2-and-twig-extensions

    It worked for me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器