Accepted answer

You can create your Twig extension with a custom filter (e.g. named jscode) with your logic and mark it safe for html, and then just use:

{% value|jscode %}


The raw filter exists for a reason, so if you are completely sure of what is going to be in value, you can just keep using it.

An alternative filter really depends on what you want to allow or not; as @Yassine suggested, a custom filter is probably a good idea because it has the additional advantage that you can move the whole logic out of your layout.

Related Query

More Query from same tag