Becksters
Becksters

Reputation: 31

Using escape and raw in twig?

I have an article that has smart quotes in it, so I need to escape the article content. However, if I use

      article.body| e

to escape the article, the html does not show up as formatted. And if I use

      article.body| raw

the article is displayed correctly as html but the smart quotes are not escaped in the content.

Does anyone know how to escape content while correctly displaying the html?

Upvotes: 0

Views: 2922

Answers (2)

Sandeep Gupta
Sandeep Gupta

Reputation: 380

to show the special characters we can use like this {{ '<'|e }} it will print only angle angle bracket '<'

Upvotes: 0

Nicolai Fr&#246;hlich
Nicolai Fr&#246;hlich

Reputation: 52513

You can specify a strategy for the escape filter.

'html' won't work for you here because it internally uses PHP's htmlspecialchars

'html' is the default strategy when using {{ var|e }}

You will need to create a custom escaping strategy for your use case.

... but if it's only the quotes which need to be replaced you could do a simple preg_replace on PrePersist and PreUpdate using Doctrine's Lifecyle Events.

Upvotes: 1

Related Questions