I already learned how to create a Twig extension.
But now I need to create a tag, because I want to do something like this:
{% singleline %}
<div>
<script>
function bla() {
alert('bla');
}
</script>
</div>
{% endsingleline %}
Result:
<div><script>function bla() { alert('bla'); }</script></div>
Any ideas?
You can do it by overriding
getTokenParsers()method in your Twig Extension class, as described here.To get a good example on how it’s done, take a look at Twig Extensions repository.
Specifically, take Debug as an example (here’s the Twig_Extensions_TokenParser_Debug class that’s passed to the method).
P.S. That said, you should take a look at the note added to Spaceless documentation page and maybe look into 3-rd party libraries like Tidy.