StringLoader.php

Same filename and directory in other branches
  1. 9 core/lib/Drupal/Core/Template/Loader/StringLoader.php
  2. 10 core/lib/Drupal/Core/Template/Loader/StringLoader.php
  3. 11.x core/lib/Drupal/Core/Template/Loader/StringLoader.php

Namespace

Drupal\Core\Template\Loader

File

core/lib/Drupal/Core/Template/Loader/StringLoader.php

View source
<?php

namespace Drupal\Core\Template\Loader;

use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
use Twig\Source;

/**
 * Loads string templates, also known as inline templates.
 *
 * This loader is intended to be used in a Twig loader chain and whitelists
 * string templates that begin with the following comment:
 * @code
 * {# inline_template_start #}
 * @endcode
 *
 * This class override ensures that the string loader behaves as expected in
 * the loader chain. If Twig's string loader is used as is, any string (even a
 * reference to a file-based Twig template) is treated as a valid template and
 * is rendered instead of a \Twig_Error_Loader exception being thrown.
 *
 * @see \Drupal\Core\Template\TwigEnvironment::renderInline()
 * @see \Drupal\Core\Render\Element\InlineTemplate
 * @see twig_render_template()
 */
class StringLoader implements \Twig_LoaderInterface, ExistsLoaderInterface, SourceContextLoaderInterface {
    
    /**
     * {@inheritdoc}
     */
    public function exists($name) {
        if (strpos($name, '{# inline_template_start #}') === 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSource($name) {
        return $name;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheKey($name) {
        return $name;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isFresh($name, $time) {
        return TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSourceContext($name) {
        $name = (string) $name;
        return new Source($name, $name);
    }

}

Classes

Title Deprecated Summary
StringLoader Loads string templates, also known as inline templates.

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