function TwigMarkupInterfaceTest::renderObjectWithTwig

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest::renderObjectWithTwig()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest::renderObjectWithTwig()
  3. 11.x core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest::renderObjectWithTwig()

Return value

\Drupal\Component\Render\MarkupInterface The rendered HTML.

2 calls to TwigMarkupInterfaceTest::renderObjectWithTwig()
TwigMarkupInterfaceTest::testEmptyTranslation in core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php
Tests behavior if a string is translated to become an empty string.
TwigMarkupInterfaceTest::testMarkupInterfaceEmpty in core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php
@dataProvider providerTestMarkupInterfaceEmpty

File

core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php, line 79

Class

TwigMarkupInterfaceTest
Tests Twig with MarkupInterface objects.

Namespace

Drupal\KernelTests\Core\Theme

Code

protected function renderObjectWithTwig($variable) {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $context = new RenderContext();
    return $renderer->executeInRenderContext($context, function () use ($renderer, $variable) {
        $elements = [
            '#type' => 'inline_template',
            '#template' => '{%- if variable is not empty -%}<span>{{ variable }}</span>{%- endif -%}',
            '#context' => [
                'variable' => $variable,
            ],
        ];
        return $renderer->render($elements);
    });
}

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