.. note::
- The ``debug`` function is only available when Twig ``debug`` mode is set.
+ The ``debug`` function is not available by default. You must load it explicitly::
+
+ $twig = new Twig_Environment($loader, $config);
+ $twig->addExtension(new Twig_Extension_Debug());
+
+ Even when loaded explicitly, it won't do anything if the ``debug`` option
+ is not enabled.
In an HTML context, wrap the output with a ``pre`` tag to make it easier to
read:
'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']),
'optimizer' => new Twig_Extension_Optimizer($options['optimizations']),
);
- if ($this->debug) {
- $this->extensions[] = new Twig_Extension_Debug();
- }
$this->strictVariables = (bool) $options['strict_variables'];
$this->runtimeInitialized = false;
$this->setCache($options['cache']);
$isDumpOutputHtmlSafe = extension_loaded('xdebug') && get_cfg_var('xdebug.overload_var_dump') && get_cfg_var('html_errors');
return array(
- 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true)),
+ 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
);
}
}
}
-function twig_var_dump($context)
+function twig_var_dump(Twig_Environment $env, $context)
{
+ if (!$env->isDebug()) {
+ return;
+ }
+
ob_start();
$count = func_num_args();
- if (1 === $count) {
+ if (2 === $count) {
$vars = array();
foreach ($context as $key => $value) {
if (!$value instanceof Twig_Template) {
var_dump($vars);
} else {
- for ($i = 1; $i < $count; $i++) {
+ for ($i = 2; $i < $count; $i++) {
var_dump(func_get_arg($i));
}
}
), $match[2] ? eval($match[2].';') : array());
$twig = new Twig_Environment($loader, $config);
$twig->addExtension(new TestExtension());
+ $twig->addExtension(new Twig_Extension_Debug());
try {
$template = $twig->loadTemplate('index.twig');