public static function TwigFactory::get

Returns a fully initialized Twig_Environment object.

This constructs and configures a Twig_Environment. It also adds Drupal specific Twig_NodeVisitors, filters and functions.

To retrieve the system wide Twig_Environment object you should use:

$twig = drupal_container()
  ->get('twig');

This will retrieve the Twig_Environment object from the DIC.

Return value

Twig_Environment The fully initialized Twig_Environment object.

See also

twig_render

TwigNodeVisitor

TwigReference

TwigReferenceFunction

File

drupal/core/lib/Drupal/Core/Template/TwigFactory.php, line 44
Definition of Drupal\Core\Template\TwigFactory.

Class

TwigFactory
A class for constructing Twig_Environment objects.

Namespace

Drupal\Core\Template

Code

public static function get() {

  // @todo Maybe we will have our own loader later.
  $loader = new \Twig_Loader_Filesystem(DRUPAL_ROOT);
  $twig = new TwigEnvironment($loader, array(
    // This is saved / loaded via drupal_php_storage().
    // All files can be refreshed by clearing caches.
    // @todo ensure garbage collection of expired files.
    'cache' => TRUE,
    'base_template_class' => 'Drupal\\Core\\Template\\TwigTemplate',
    // @todo Remove in followup issue
    // @see http://drupal.org/node/1712444.
    'autoescape' => FALSE,
    // @todo Remove in followup issue
    // @see http://drupal.org/node/1806538.
    'strict_variables' => FALSE,
    // @todo Maybe make debug mode dependent on "production mode" setting.
    'debug' => TRUE,
    // @todo Make auto reload mode dependent on "production mode" setting.
    'auto_reload' => FALSE,
  ));

  // The node visitor is needed to wrap all variables with
  // render -> twig_render() function.
  $twig
    ->addNodeVisitor(new TwigNodeVisitor());
  $twig
    ->addTokenParser(new TwigFunctionTokenParser('hide'));
  $twig
    ->addTokenParser(new TwigFunctionTokenParser('show'));

  // @todo Figure out what to do about debugging functions.
  // @see http://drupal.org/node/1804998
  $twig
    ->addExtension(new \Twig_Extension_Debug());
  $reference_functions = array(
    'hide' => 'twig_hide',
    'render' => 'twig_render',
    'show' => 'twig_show',
  );
  $filters = array(
    't' => 't',
  );

  // These functions will receive a TwigReference object, if a render array is detected
  foreach ($reference_functions as $function => $php_function) {
    $twig
      ->addFunction($function, new TwigReferenceFunction($php_function));
  }
  foreach ($filters as $filter => $php_function) {
    $twig
      ->addFilter($filter, new \Twig_Filter_Function($php_function));
  }

  // @todo Remove URL function once http://drupal.org/node/1778610 is resolved.
  $twig
    ->addFunction('url', new \Twig_Function_Function('url'));
  return $twig;
}