Reorganize array in twig

First a FYI though, this is not something you should be doing in the template, cause the code is pretty messy. Anyway here is how you merge/group arrays in twig: {% set output = [] %} {% for item in items %} {% if not attribute(output, item.object.category) is defined %} {% set output = output|merge({ … Read more

Set value of single object in multidimensional array in twig template

To change a certain index of an array you’re best of with extending Twig, a solution could be this, ProjectTwigExtension.php namespace Your\Namespace; class ProjectTwigExtension extends Twig_Extension { public function getFunctions() { return array( new Twig_SimpleFunction(‘set_array_value’, array($this, ‘setArrayValue’), [‘needs_context’ => true,]), new Twig_SimpleFunction(‘set_object_property’, array($this, ‘setArrayValue’), [‘needs_context’ => true,]), ); } public function setArrayValue(&$context, $array_name, $index, $value) … Read more

Twig: render vs include

Each render call spawns a new request, with the performance degradation issue that you are describing. I don’t think there is much you can do about that but using esi caching, so that single fragments coming from render calls can be cached. Otherwise you could try to revise your logic to reduce the usage of … Read more

How to render a tree in Twig

I played around with domi27’s idea and came up with this. I made a nested array as my tree, [‘link’][‘sublinks’] is null or another array of more of the same. Templates The sub-template file to recurse with: <!–includes/menu-links.html–> {% for link in links %} <li> <a href=”https://stackoverflow.com/questions/8326482/{{ link.href }}”>{{ link.name }}</a> {% if link.sublinks %} … Read more

Extends parent blocks from embed template

As said in the comment, includes/embeds can’t alter blocks from their includer. That said there is an extension available that could solve your problem. This Deferred Twig Extension can be found here Basically the node postpones the execution of a said block. This way you can create a variable that holds all of your javascript … Read more

Twig variables in twig variable

I have twig variable html. To show it in twig template I do {{html}}. That variable look like {{region_top}}{{region_center}}. region_* is variables too. When twig parse my html variable he didn’t parse inner variables (regions). What can I should do? Twig takes your strings as a literal string, meaning you’ll see the content of the … Read more