function twig_render

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.

Parameters

mixed $arg: String, Object or Render Array

Return value

The rendered output or an Twig_Markup object.

See also

render

TwigNodeVisitor

1 string reference to 'twig_render'
TwigFactory::get in drupal/core/lib/Drupal/Core/Template/TwigFactory.php
Returns a fully initialized Twig_Environment object.

File

drupal/core/themes/engines/twig/twig.engine, line 72
Handles integration of Twig templates with the Drupal theme system.

Code

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);
}