Symfony each block with a space

How can I wrap each block code without spaces to trim spaces 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 tries to display it, I want symfony to see

 {% 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 %} 
+4
source share
1 answer

Define your own Twig tag (copy and paste method)

You can define your own Twig spacelessblock tag that combines block and spaceless . Then you can use {% spacelessblock xyz %}โ€ฆ{% endspacelessblock %} in your templates. Here's how you do it fast and dirty (copy and paste).

New Twig node

First, define the Twig_Node_SpacelessBlock class (for example, in the Extension directory of your package):

 class Twig_Node_SpacelessBlock extends \Twig_Node_Block { public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null) { parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag); } public function compile(Twig_Compiler $compiler) { // top part of Block.compile $compiler ->addDebugInfo($this) ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n") ->indent() ; // the content of the body is treated like in Spaceless.compile $compiler ->write("ob_start();\n") ->subcompile($this->getNode('body')) ->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n") ; // bottom part of Block.compile $compiler ->outdent() ->write("}\n\n") ; } } 

New Twig Token Analyzer

Our new Twig node should be created anywhere when Twig finds {% spacelessblock xyz %} in the template. To do this, we need a parser marker, which we call Twig_TokenParser_SpacelessBlock . We basically copy and paste Twig_TokenParser_Block :

 class Twig_TokenParser_SpacelessBlock extends \Twig_TokenParser { public function parse(Twig_Token $token) { // โ€ฆ $this->parser->setBlock($name, $block = new Twig_Node_SpacelessBlock($name, new Twig_Node(array()), $lineno)); // โ€ฆ } public function decideBlockEnd(Twig_Token $token) { return $token->test('endspacelessblock'); } public function getTag() { return 'spacelessblock'; } } 

Tell us about it Twig

In your extension class:

 class Extension extends \Twig_Extension { public function getTokenParsers() { return array( new Twig_TokenParser_SpacelessBlock(), ); } } 

Report this to Symfony

If not already done, add the following to your services.yml :

 services: # โ€ฆ my.extension: class: Acme\MyBundle\Extension\Extension tags: - { name: twig.extension } 

Best alternatives

Preprocessor

It is best to use a preprocessor to simply replace

 {% spacelessblock xyz %} โ€ฆ {% endspacelessblock %} 

 {% block xyz %}{% spaceless %} โ€ฆ {% endspaceless %}{% endblock %} 

which reuses all the code that has already been recorded in the Twig project, including possible changes.

+5
source

Source: https://habr.com/ru/post/1440535/


All Articles