Skip to content

Conversation

@PatchRanger
Copy link
Contributor

@samdark
Copy link
Member

samdark commented Mar 30, 2015

How to use it?

@samdark
Copy link
Member

samdark commented Mar 30, 2015

Also it seems it breaks tests: https://travis-ci.org/yiisoft/yii2-twig/jobs/56381597#L158

@PatchRanger
Copy link
Contributor Author

Here is how I use it, place this to configuration array:

    'components'=>[
             'view' => [
                'renderers' => [
                    'twig' => [
                        'class' => 'yii\twig\ViewRenderer',
                        // set cachePath to false in order to disable template caching
                        'cachePath' => '@runtime/Twig/cache',
                        // Array of twig options:
                        'options' => [
                            'auto_reload' => true,
                            'autoescape' => true,
                            //'debug' => true,
                        ],
                        // ... see ViewRenderer for more options
                        'extensions'=>[
                            // In order to make dump() to output to the screen, we also need to set options[debug] = true.
                            'Twig_Extension_Debug',
                        ],
                        'globals'=>[
                            'html' => 'CHtml'
                        ],
                        'functions'=>[
                            'staticCall'=>'call_user_func_array',
                            'static'=>function ($class, $property) {
                                return property_exists($class, $property)
                                    // Static variable.
                                    ? $class::$$property
                                    : (defined("$class::$property")
                                        // Class constant.
                                        ? constant("$class::$property")
                                        : null);
                            },
                            'isInstanceOf'=>function($object,$className) {
                                return ($object instanceof $className);
                            },
                            'setProperty'=>function($object,$key,$value) {
                                // In order to tame low-level error, which is very hard to debug otherwise - only "Creating default object from empty value".
                                if (empty($object)) {
                                    throw new CException('Trying to set a property of empty object');
                                }
                                $object->{$key} = $value;
                            },
                            'unset'=>function($variable) {
                                unset($variable);
                            },
                            'arrayUnset'=>function($array, $key) {
                                unset($array[$key]);
                                return $array;
                            },
                            'new'=>function($className,$args=[]) {
                                // Only the first 3 parameters - for simplicity and safe.
                                $args = (array)$args;
                                $arg1 = array_shift($args);
                                $args = (array)$args;
                                $arg2 = array_shift($args);
                                $args = (array)$args;
                                $arg3 = array_shift($args);
                                $args = (array)$args;
                                if ($arg3) {
                                    return new $className($arg1,$arg2,$arg3);
                                }
                                elseif ($arg2) {
                                    return new $className($arg1,$arg2);
                                }
                                elseif ($arg1) {
                                    return new $className($arg1);
                                }
                                else {
                                    return new $className();
                                }
                            },
                            // To debug deep places of Twig templates.
                            'throw'=>function(Exception $exception) {
                                throw $exception;
                            },
                            // Helper.
                            'print_r' => function($value, $return=true) {
                                return print_r($value,$return);
                            },
                        ],
                    ],
            ],
        ],
    ],

@samdark samdark added the type:enhancement Enhancement label Mar 31, 2015
@samdark samdark self-assigned this Mar 31, 2015
@samdark samdark closed this May 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:enhancement Enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants