Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 14 |
| InlineTemplate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 14 |
| getInfo | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 9 |
|||
| preRenderInlineTemplate | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 5 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\Core\Render\Element\InlineTemplate. | |
| */ | |
| namespace Drupal\Core\Render\Element; | |
| /** | |
| * Provides a render element where the user supplies an in-line Twig template. | |
| * | |
| * Properties: | |
| * - #template: The inline Twig template used to render the element. | |
| * - #context: (array) The variables to substitute into the Twig template. | |
| * Each variable may be a string or a render array. | |
| * | |
| * Usage example: | |
| * @code | |
| * $build['hello'] = [ | |
| * '#type' => 'inline_template', | |
| * '#template' => "{% trans %} Hello {% endtrans %} <strong>{{name}}</strong>", | |
| * '#context' => [ | |
| * 'name' => $name, | |
| * ] | |
| * ]; | |
| * @endcode | |
| * | |
| * @RenderElement("inline_template") | |
| */ | |
| class InlineTemplate extends RenderElement { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getInfo() { | |
| $class = get_class($this); | |
| return array( | |
| '#pre_render' => array( | |
| array($class, 'preRenderInlineTemplate'), | |
| ), | |
| '#template' => '', | |
| '#context' => array(), | |
| ); | |
| } | |
| /** | |
| * 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; | |
| } | |
| } |