You are here

protected function TwigMarkupInterfaceTest::renderObjectWithTwig in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest::renderObjectWithTwig()
  2. 9 core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest::renderObjectWithTwig()

Return value

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

File

core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php, line 84
Contains \Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest.

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);
  });
}