abstract class TwigTemplate

This is the base class for compiled Twig templates.

Hierarchy

  • class \Drupal\Core\Template\TwigTemplate extends \Drupal\Core\Template\Twig_Template

Expanded class hierarchy of TwigTemplate

File

drupal/core/lib/Drupal/Core/Template/TwigTemplate.php, line 13
Definition of Drupal\Core\Template\TwigTemplate.

Namespace

Drupal\Core\Template
View source
abstract class TwigTemplate extends \Twig_Template {

  /**
   * A class used to pass variables by reference while they are used in Twig.
   */
  protected $twig_reference = NULL;

  /**
   * List of the name of variables to be passed around as references.
   *
   * @var array
   */
  protected $is_reference = array();

  /**
   * List of the name of variables to be passed around by value.
   *
   * @var array
   */
  protected $is_no_reference = array();

  /**
   * @param array $context
   *   The variables available to the template.
   * @param $item
   *   The name of the variable.
   * @return mixed
   *   The requested variable.
   */
  protected final function getContextReference(&$context, $item) {

    // Optimized version
    if (!isset($context[$item])) {

      // We don't want to throw an exception, but issue a warning instead.
      // This is the easiest way to do so.
      // @todo Decide based on prod vs. dev setting
      $msg = new \Twig_Error(t('@item could not be found in _context', array(
        '@item' => $item,
      )));
      trigger_error($msg
        ->getMessage(), E_USER_WARNING);
      return NULL;
    }

    // The first test also finds empty / null render arrays
    if (!$context[$item] || isset($this->is_no_reference[$item])) {
      return $context[$item];
    }
    if (isset($context['_references'][$item])) {
      return $context['_references'][$item];
    }

    // @todo Check if this is a render array (existence of #theme?)
    if (!isset($this->is_reference[$item]) && ($context[$item] instanceof \TwigMarkup || !is_array($context[$item]))) {
      $this->is_no_reference[$item] = TRUE;
      return $context[$item];
    }
    if ($this->twig_reference == NULL) {
      $this->twig_reference = new TwigReference();
    }
    $ref = clone $this->twig_reference;

    // clone is _much_ faster than new
    $ref
      ->setReference($context[$item]);

    // Save that this is a reference
    $context['_references'][$item] = $ref;
    $this->is_reference[$item] = TRUE;
    return $ref;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwigTemplate::$is_no_reference protected property List of the name of variables to be passed around by value.
TwigTemplate::$is_reference protected property List of the name of variables to be passed around as references.
TwigTemplate::$twig_reference protected property A class used to pass variables by reference while they are used in Twig.
TwigTemplate::getContextReference final protected function