Question

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.
Was it helpful?

Solution

Have a look at this manual:

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

It worked for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top