function TwigAttributesTest::testYield

Tests that Attributes are rendered at the correct time within a macro.

File

core/tests/Drupal/KernelTests/Core/Theme/TwigAttributesTest.php, line 20

Class

TwigAttributesTest
Tests Twig with Attribute objects.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testYield() : void {
  /** @var \Drupal\Core\Template\TwigEnvironment $environment */
  $environment = \Drupal::service('twig');
  $template = '{{ var }}{% set var = var + 1 %}{{ var }}';
  $var = 0;
  $this->assertEquals('01', $environment->renderInline($template, [
    'var' => $var,
  ]));
  $this->assertEquals('01', $environment->renderInline($template, [
    'var' => &$var,
  ]));
  $template = '{{ attributes.addClass("test2") }}{% set attributes = attributes.removeClass("test2") %}{{ attributes }}';
  $attributes = new Attribute([
    'class' => 'test1',
  ]);
  $this->assertEquals(' class="test1 test2" class="test1"', $environment->renderInline($template, [
    'attributes' => $attributes,
  ]));
  $template = '{{ _self.macro_test(var) }}{% macro macro_test(var) %}{{ var }}{% set var = var + 1 %}{{ var }}{% endmacro %}';
  $var = 0;
  $this->assertEquals('01', $environment->renderInline($template, [
    'var' => $var,
  ]));
  $this->assertEquals('01', $environment->renderInline($template, [
    'var' => &$var,
  ]));
  $template = '{{ _self.macro_test(attributes) }}{% macro macro_test(attributes) %}{{ attributes.addClass("test2") }}{% set attributes = attributes.removeClass("test2") %}{{ attributes }}{% endmacro %}';
  $attributes = new Attribute([
    'class' => 'test1',
  ]);
  $this->assertEquals(' class="test1 test2" class="test1"', $environment->renderInline($template, [
    'attributes' => $attributes,
  ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.