in a former post i give a solution to make var_dump in twig, the main idea is set twig into debug mode and debug a variable, but Symfony2 or such kind of framework make it impossible, the circle of reference make the dump infinitive. So we need to don in another way:
First , we need to create a service which can be called by a twig file, the tutorial can see here : symfony2 offical service creatation for twig extension
in your service.xml
and then :
Then you can use this extension in your twig template:
First , we need to create a service which can be called by a twig file, the tutorial can see here : symfony2 offical service creatation for twig extension
in your service.xml
and then :
/**
* TVarDumper class.
*
* TVarDumper is intended to replace the buggy PHP function var_dump and print_r.
* It can correctly identify the recursively referenced objects in a complex
* object structure. It also has a recursive depth control to avoid indefinite
* recursive display of some peculiar variables.
*
* TVarDumper can be used as follows,
*
* echo TVarDumper::dump($var);
*
*
* @author Qiang Xue qiang.xue@gmail.com
* @version $Id$
* @package System.Util
* @since 3.0
* @modifiedby: ferdinandfly@gmail.com
*/
namespace Acme\UserBundle\ExtraFunctions;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig_Extension;
class TVarDumper extends Twig_Extension
{
private $_objects;
private $_output;
private $_depth;
public function __construct()
{
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
'mydump' => new \Twig_Function_Method($this, 'dump')
);
}
/**
* Converts a variable into a string representation.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as PRADO controls.
* @param mixed variable to be dumped
* @param integer maximum depth that the dumper should go into the variable. Defaults to 10.
* @return string the string representation of the variable
*/
public function dump($var,$depth=10,$highlight=false)
{
$this->_output='';
$this->_objects=array();
$this->_depth=$depth;
$this->dumpInternal($var,0);
if($highlight)
{
$result=highlight_string("_output,true);
return preg_replace('/<\\?php
/','',$result,1);
}
else
return $this->_output;
}
private function dumpInternal($var,$level)
{
switch(gettype($var))
{
case 'boolean':
$this->_output.=$var?'true':'false';
break;
case 'integer':
$this->_output.="$var";
break;
case 'double':
$this->_output.="$var";
break;
case 'string':
$this->_output.="'$var'";
break;
case 'resource':
$this->_output.='{resource}';
break;
case 'NULL':
$this->_output.="null";
break;
case 'unknown type':
$this->_output.='{unknown}';
break;
case 'array':
if($this->_depth<=$level)
$this->_output.='array(...)';
else if(empty($var))
$this->_output.='array()';
else
{
$keys=array_keys($var);
$spaces=str_repeat(' ',$level*4);
$this->_output.="array\n".$spaces.'(';
foreach($keys as $key)
{
$this->_output.="\n".$spaces." [$key] => ";
$this->_output.=$this->dumpInternal($var[$key],$level+1);
}
$this->_output.="\n".$spaces.')';
}
break;
case 'object':
if(($id=array_search($var,$this->_objects,true))!==false)
$this->_output.=get_class($var).'#'.($id+1).'(...)';
else if($this->_depth<=$level)
$this->_output.=get_class($var).'(...)';
else
{
$id=array_push($this->_objects,$var);
$className=get_class($var);
$members=(array)$var;
$keys=array_keys($members);
$spaces=str_repeat(' ',$level*4);
$this->_output.="$className#$id\n".$spaces.'(';
foreach($keys as $key)
{
$keyDisplay=strtr(trim($key),array("\0"=>':'));
$this->_output.="\n".$spaces." [$keyDisplay] => ";
$this->_output.=$this->dumpInternal($members[$key],$level+1);
}
$this->_output.="\n".$spaces.')';
}
break;
}
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'user_bundle';
}
}
Then you can use this extension in your twig template:
{{ mydump(object)}}
评论
发表评论