You are here

public function TwigEnvironment::renderInline in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Template/TwigEnvironment.php \Drupal\Core\Template\TwigEnvironment::renderInline()
  2. 10 core/lib/Drupal/Core/Template/TwigEnvironment.php \Drupal\Core\Template\TwigEnvironment::renderInline()

Renders a twig string directly.

Warning: You should use the render element 'inline_template' together with the #template attribute instead of this method directly. On top of that you have to ensure that the template string is not dynamic but just an ordinary static php string, because there may be installations using read-only PHPStorage that want to generate all possible twig templates as part of a build step. So it is important that an automated script can find the templates and extract them. This is only possible if the template is a regular string.

Parameters

string $template_string: The template string to render with placeholders.

array $context: An array of parameters to pass to the template.

Return value

\Drupal\Component\Render\MarkupInterface|string The rendered inline template as a Markup object.

See also

\Drupal\Core\Template\Loader\StringLoader::exists()

File

core/lib/Drupal/Core/Template/TwigEnvironment.php, line 232

Class

TwigEnvironment
A class that defines a Twig environment for Drupal.

Namespace

Drupal\Core\Template

Code

public function renderInline($template_string, array $context = []) {

  // Prefix all inline templates with a special comment.
  $template_string = '{# inline_template_start #}' . $template_string;
  return Markup::create($this
    ->createTemplate($template_string)
    ->render($context));
}