'inline_template', * '#template' => "{% trans %} Hello {% endtrans %} {{name}}", * '#context' => [ * 'name' => $name, * ] * ]; * @endcode * * @RenderElement("inline_template") */ class InlineTemplate extends RenderElement { /** * {@inheritdoc} */ public function getInfo() { $class = get_class($this); return [ '#pre_render' => [ [$class, 'preRenderInlineTemplate'], ], '#template' => '', '#context' => [], ]; } /** * Renders a twig string directly. * * @param array $element * * @return array */ public static function preRenderInlineTemplate($element) { /** @var \Drupal\Core\Template\TwigEnvironment $environment */ $environment = \Drupal::service('twig'); $markup = $environment->renderInline($element['#template'], $element['#context']); $element['#markup'] = $markup; return $element; } }