douxianwu2221
douxianwu2221
2014-02-20 07:55

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条回答

相关推荐