跳至主要内容

vardump in twig

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 :
/**
 * 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)}}

评论

此博客中的热门博文

记得很久以前,不知道是在跟谁发感慨,久到好像是上大学时候的事了。我说,我流过很多眼泪,看电影流眼泪,看小说流眼泪,听别人的故事留言了,听歌的时候流眼泪,晚上一个人的时候独自流眼泪。我似乎是一个是一个太不像男人的男人。可是我又说,这些眼泪都是为自己流的。于是我也问对面那个人,(嗯,我也记不起她是谁了): "你为别人流过眼泪么?",居然没有回答我. 也许看见这篇文章的人也不相信,但是我真的想问你一句,你为别人流过眼泪么?真真正正的是为别人的,不是可怜自己,不是觉得别人可怜而让自己也感到了可怜,仅仅是为了别人而悲伤,有过么?如果真的有,我想那也许才是爱吧。 也许您会觉得奇怪,这和爱有什么关系?是啊,流眼泪就是爱么?我们因为悲伤而流泪,流泪是因为,爱别离,求不得,这些的主体是什么?是自己。 我们都是爱自己的,只是99%的人是吧。所以我们流泪了,因为我们的那些种种原因,我们很少会因为别人“求不得,爱别离”而流泪吧。也许说,那又不是自己,对呀,你爱自己,你又不爱她,何苦要为她流这种泪? 我不是一个宽容的人,我也不是一个豁达的人,这是我一生的缺点。我们会为了爱去宽容,我们会因为爱而变的豁达,也许,这是因为我从来都没有真正的爱过谁吧,甚至,连自己。嘿,我现在都开始疑惑,我真的爱过你么,如果是,我为什么都没有宽容和豁达呢?

对于GFW的看法

谈到这个问题的人很多,讲的也非常详细,但我窃以为大多数人对此都不够重视。在这一点上来讲,国民党堪称楷模,我想台湾能有今日,很大程度上是因为他们最终也没有建立一个可以堪比中国的新闻审查制度。从权利集中的角度来讲,中国共产党并不比任何一个封建王朝差,从执行力度上来讲,那也和历史上最严酷的王朝可以评分秋色。至少,国民党还是讲点道德,尊重文化的。 如果说计划生育阉割了中国人的人口,那么GFW,和新闻出版署以及文化部阉割了中国人的精神。也许乐观的人说我们总可以翻墙,我们总可以通过这样那样的方法来获得信息。但是悲观的我并不这么认为,这种限制创造的是一种环境,是空气,你固然可以戴着口罩,但是你永远无法自由的呼吸,当大家都在这样的空气中活着的时候,戴着口罩的你是会被人另眼相看的。其实群体意识在掌握了所有媒体的政府面前就是一个面团,想怎么捏就怎么捏,本来就是信息不对称的更何况还是受到随意控制的。这是无解的,中国亟待提高公民意识,哲学思想,文化水平,等等,政府做的不过是又一轮的愚民罢了。鲁迅先生为之殚精极虑,奋斗一生的东西,过了一个世纪依然没有什么本质的改变,阿Q在国人里占的比例是在太大了。就好比是我以诚待国人,以心侍之,以身献之,奈何国人谓之以“傻逼”。你却又待何如?这样的文化封锁,信息封锁导致的唯一结果就是,你振臂一呼之时,便是阿Q们结队去看你被看头之日。 其实,中国政府早已脱离了共产主义这个哲学思想了。换句话说,这个哲学思想本身是没什么对错的,只是一旦应用起来就满不是那么回事了,几乎可以说,共产主义的集权必然导致共产主义的被抛弃。

chrome extension Error: attempting to use a disconnected prot object

if you get this error: Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:236 chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:236 Uncaught Error: Attempting to use a disconnected port object miscellaneous_bindings:58 PortImpl.postMessage miscellaneous_bindings:58 responseCallback miscellaneous_bindings:143 xhr.onreadystatechange That means you have make some mistake as this discussed: This is caused when a connection get closed. For example if you open a tab that has the content_script injected, it opens a connection, the tab is closed, and then the background_page tries to pass a message. It will fail because the tab is no longer active to receive the message. In your case I would guess that as tabs close and new tabs open you are attempting to post messages with the old tabId instead of creating a new connection to the new tab. I would recommend reading through the  long-lived connectio...