function twig_render_var

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_var'
TwigExtension::getFunctions in drupal/core/lib/Drupal/Core/Template/TwigExtension.php

File

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

Code

function twig_render_var($arg) {
  if ($arg instanceof TwigReference) {
    $arg =& $arg
      ->getReference();
  }

  // Check for numeric zero.
  if ($arg === 0) {
    return 0;
  }

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