How i can wrap every block code with spaceless to crop whitespaces from my twig/html
for example now i have:
{% block content %}
<div class="box clearfix clearall">
<div class="ct colcontainer">
<div class="col-1">
<div class="chars">
<table class="layout data-char">
<thead>
blabla
{% endblock %}
And when symfony try to render it, i want that symfony saw
{% block content %}
{% spaceless %}
<div class="box clearfix clearall">
<div class="ct colcontainer">
<div class="col-1">
<div class="chars">
<table class="layout data-char">
<thead>
blabla
{% endspaceless %}
{% endblock %}
Define a custom Twig tag (the copy-and-paste way)
You can define a custom Twig tag
spacelessblockwhich combinesblockandspaceless. Then you can use{% spacelessblock xyz %}…{% endspacelessblock %}in your templates. Here is how you do it the quick and dirty (copy and paste) way.A new Twig node
First, define a class
Twig_Node_SpacelessBlock(e.g. in theExtensiondirectory of your bundle):A new Twig token parser
Our new Twig node needs to be built somewhere whenever Twig finds a
{% spacelessblock xyz %}in a template. For that, we need a token parser which we callTwig_TokenParser_SpacelessBlock. We basically copy and pasteTwig_TokenParser_Block:Tell Twig about it
In your extension class:
Tell Symfony about it
If not already done, add the following to your your
services.yml:Better alternatives
Preprocessor
A better way would be to use a preprocessor to simply replace
by
which reuses all the code that already has been written in the Twig project, including possible changes.