diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 74d1e91..a2d931d 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -2,6 +2,7 @@ namespace Drupal\twig_tweak; +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Unicode; use Drupal\Component\Uuid\Uuid; use Drupal\Core\Block\BlockPluginInterface; @@ -720,7 +721,7 @@ class TwigExtension extends \Twig_Extension { * * @param array $build * The renderable array to add the child item. - * @param int|string $key + * @param mixed $key * The key of the new element. * @param mixed $element * The element to add. @@ -729,7 +730,12 @@ class TwigExtension extends \Twig_Extension { * The modified array. */ public function with(array $build, $key, $element) { - $build[$key] = $element; + if (is_array($key)) { + NestedArray::setValue($build, $key, $element); + } + else { + $build[$key] = $element; + } return $build; } diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 4789d69..3ea7115 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -309,6 +309,10 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-with"]/b[text() = "Example"]'; $this->assertByXpath($xpath); + // Test nested 'with'. + $xpath = '//div[@class = "tt-with-nested" and text() = "{alpha:{beta:{gamma:456}}}"]'; + $this->assertByXpath($xpath); + // Test 'children'. $xpath = '//div[@class = "tt-children" and text() = "doremi"]'; $this->assertByXpath($xpath); diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index 759e468..128a768 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -59,6 +59,7 @@
{{ 'bold strong' | check_markup('twig_tweak_test') }}
{{ 'Hello world!'|truncate(10, true, true) }}
{{ {'#markup':'Example'}|with('#prefix', '')|with('#suffix', '') }}
+
{{ {alpha: {beta: {gamma: 123}}}|with(['alpha', 'beta', 'gamma'], 456)|json_encode|replace({'"':''}) }}
{%- set build = {