twig.engine

Handles integration of Twig templates with the Drupal theme system.

File

drupal/core/themes/engines/twig/twig.engine
View source
<?php

/**
 * @file
 * Handles integration of Twig templates with the Drupal theme system.
 */
use Drupal\Core\Template\TwigReference;

/**
 * Implements hook_theme().
 */
function twig_theme($existing, $type, $theme, $path) {
  return drupal_find_theme_templates($existing, '.twig', $path);
}

/**
 * Implements hook_extension().
 */
function twig_extension() {
  return '.twig';
}

/**
 * Implements hook_init().
 */
function twig_init($template) {
  $file = dirname($template->filename) . '/template.php';
  if (file_exists($file)) {
    include_once DRUPAL_ROOT . '/' . $file;
  }
}

/**
 * Render twig templates.
 *
 * This retrieves the Twig_Environment from the Drupal Injection container and
 * renders the template.
 *
 * @param $template_file
 *   The filename of the template to render.
 * @param $variables
 *   A keyed array of variables that will appear in the output.
 *
 * @return
 *   The output generated by the template.
 */
function twig_render_template($template_file, $variables) {
  $variables['_references'] = array();
  return drupal_container()
    ->get('twig')
    ->loadTemplate($template_file)
    ->render($variables);
}

/**
 * Wrapper around render() for twig printed output.
 *
 * If an object is passed that has no __toString method an exception is thrown;
 * other objects are casted to string. However in the case that the object is an
 * instance of a Twig_Markup object it is returned directly to support auto
 * escaping.
 *
 * If an array is passed it is rendered via render() and scalar values are
 * returned directly.
 *
 * @param mixed $arg
 *   String, Object or Render Array
 *
 * @return
 *   The rendered output or an Twig_Markup object.
 *
 * @see render
 * @see TwigNodeVisitor
 */
function twig_render($arg) {
  if ($arg instanceof TwigReference) {
    $arg =& $arg
      ->getReference();
  }

  // == is true also for empty arrays
  if ($arg == NULL) {
    return NULL;
  }

  // Keep Twig_Markup objects intact to prepare for later autoescaping support
  if ($arg instanceof Twig_Markup) {
    return $arg;
  }
  if (is_scalar($arg)) {
    return $arg;
  }
  if (is_object($arg)) {
    if (method_exists($arg, '__toString')) {
      return (string) $arg;
    }
    throw new Exception(t('Object of type "@class" cannot be printed.', array(
      '@class' => get_class($arg),
    )));
  }

  // This is a normal render array.
  return render($arg);
}

/**
 * Wrapper around hide() that does not return the content.
 *
 * @see hide
 */
function twig_hide($element) {
  if ($element instanceof TwigReference) {
    $element =& $element
      ->getReference();
    hide($element);
  }

  // @todo Add warning in else case
}

/**
 * Wrapper around show() that does not return the content.
 *
 * @see show
 */
function twig_show($element) {
  if ($element instanceof TwigReference) {
    $element =& $element
      ->getReference();
    show($element);
  }

  // @todo Add warning in else case
}

Functions

Namesort descending Description
twig_extension Implements hook_extension().
twig_hide Wrapper around hide() that does not return the content.
twig_init Implements hook_init().
twig_render Wrapper around render() for twig printed output.
twig_render_template Render twig templates.
twig_show Wrapper around show() that does not return the content.
twig_theme Implements hook_theme().