... | ... |
@@ -137,13 +137,13 @@ Creating a filter is as simple as associating a name with a PHP callable:: |
137 | 137 |
$filter = new Twig_SimpleFilter('rot13', 'str_rot13'); |
138 | 138 |
|
139 | 139 |
// or a class static method |
140 |
- $filter = new Twig_SimpleFilter('rot13', array('SomeClass', 'rot13Filter')); |
|
140 |
+ $filter = new Twig_SimpleFilter('rot13', ['SomeClass', 'rot13Filter']); |
|
141 | 141 |
$filter = new Twig_SimpleFilter('rot13', 'SomeClass::rot13Filter'); |
142 | 142 |
|
143 | 143 |
// or a class method |
144 |
- $filter = new Twig_SimpleFilter('rot13', array($this, 'rot13Filter')); |
|
144 |
+ $filter = new Twig_SimpleFilter('rot13', [$this, 'rot13Filter']); |
|
145 | 145 |
// the one below needs a runtime implementation (see below for more information) |
146 |
- $filter = new Twig_SimpleFilter('rot13', array('SomeClass', 'rot13Filter')); |
|
146 |
+ $filter = new Twig_SimpleFilter('rot13', ['SomeClass', 'rot13Filter']); |
|
147 | 147 |
|
148 | 148 |
The first argument passed to the ``Twig_SimpleFilter`` constructor is the name |
149 | 149 |
of the filter you will use in templates and the second one is the PHP callable |
... | ... |
@@ -195,7 +195,7 @@ environment as the first argument to the filter call:: |
195 | 195 |
$charset = $env->getCharset(); |
196 | 196 |
|
197 | 197 |
return str_rot13($string); |
198 |
- }, array('needs_environment' => true)); |
|
198 |
+ }, ['needs_environment' => true]); |
|
199 | 199 |
|
200 | 200 |
Context-aware Filters |
201 | 201 |
~~~~~~~~~~~~~~~~~~~~~ |
... | ... |
@@ -207,11 +207,11 @@ the first argument to the filter call (or the second one if |
207 | 207 |
|
208 | 208 |
$filter = new Twig_SimpleFilter('rot13', function ($context, $string) { |
209 | 209 |
// ... |
210 |
- }, array('needs_context' => true)); |
|
210 |
+ }, ['needs_context' => true]); |
|
211 | 211 |
|
212 | 212 |
$filter = new Twig_SimpleFilter('rot13', function (Twig_Environment $env, $context, $string) { |
213 | 213 |
// ... |
214 |
- }, array('needs_context' => true, 'needs_environment' => true)); |
|
214 |
+ }, ['needs_context' => true, 'needs_environment' => true]); |
|
215 | 215 |
|
216 | 216 |
Automatic Escaping |
217 | 217 |
~~~~~~~~~~~~~~~~~~ |
... | ... |
@@ -221,14 +221,14 @@ before printing. If your filter acts as an escaper (or explicitly outputs HTML |
221 | 221 |
or JavaScript code), you will want the raw output to be printed. In such a |
222 | 222 |
case, set the ``is_safe`` option:: |
223 | 223 |
|
224 |
- $filter = new Twig_SimpleFilter('nl2br', 'nl2br', array('is_safe' => array('html'))); |
|
224 |
+ $filter = new Twig_SimpleFilter('nl2br', 'nl2br', ['is_safe' => ['html']]); |
|
225 | 225 |
|
226 | 226 |
Some filters may need to work on input that is already escaped or safe, for |
227 | 227 |
example when adding (safe) HTML tags to originally unsafe output. In such a |
228 | 228 |
case, set the ``pre_escape`` option to escape the input data before it is run |
229 | 229 |
through your filter:: |
230 | 230 |
|
231 |
- $filter = new Twig_SimpleFilter('somefilter', 'somefilter', array('pre_escape' => 'html', 'is_safe' => array('html'))); |
|
231 |
+ $filter = new Twig_SimpleFilter('somefilter', 'somefilter', ['pre_escape' => 'html', 'is_safe' => ['html']]); |
|
232 | 232 |
|
233 | 233 |
Variadic Filters |
234 | 234 |
~~~~~~~~~~~~~~~~ |
... | ... |
@@ -240,9 +240,9 @@ When a filter should accept an arbitrary number of arguments, set the |
240 | 240 |
``is_variadic`` option to ``true``; Twig will pass the extra arguments as the |
241 | 241 |
last argument to the filter call as an array:: |
242 | 242 |
|
243 |
- $filter = new Twig_SimpleFilter('thumbnail', function ($file, array $options = array()) { |
|
243 |
+ $filter = new Twig_SimpleFilter('thumbnail', function ($file, array $options = []) { |
|
244 | 244 |
// ... |
245 |
- }, array('is_variadic' => true)); |
|
245 |
+ }, ['is_variadic' => true]); |
|
246 | 246 |
|
247 | 247 |
Be warned that named arguments passed to a variadic filter cannot be checked |
248 | 248 |
for validity as they will automatically end up in the option array. |
... | ... |
@@ -285,7 +285,7 @@ deprecated one when that makes sense:: |
285 | 285 |
|
286 | 286 |
$filter = new Twig_SimpleFilter('obsolete', function () { |
287 | 287 |
// ... |
288 |
- }, array('deprecated' => true, 'alternative' => 'new_one')); |
|
288 |
+ }, ['deprecated' => true, 'alternative' => 'new_one']); |
|
289 | 289 |
|
290 | 290 |
When a filter is deprecated, Twig emits a deprecation notice when compiling a |
291 | 291 |
template using it. See :ref:`deprecation-notices` for more information. |
... | ... |
@@ -343,7 +343,7 @@ This is used by many of the tests built into Twig:: |
343 | 343 |
$test = new Twig_SimpleTest( |
344 | 344 |
'odd', |
345 | 345 |
null, |
346 |
- array('node_class' => 'Twig_Node_Expression_Test_Odd')); |
|
346 |
+ ['node_class' => 'Twig_Node_Expression_Test_Odd']); |
|
347 | 347 |
$twig->addTest($test); |
348 | 348 |
|
349 | 349 |
class Twig_Node_Expression_Test_Odd extends Twig_Node_Expression_Test |
... | ... |
@@ -486,7 +486,7 @@ The ``Project_Set_Node`` class itself is rather simple:: |
486 | 486 |
{ |
487 | 487 |
public function __construct($name, Twig_Node_Expression $value, $line, $tag = null) |
488 | 488 |
{ |
489 |
- parent::__construct(array('value' => $value), array('name' => $name), $line, $tag); |
|
489 |
+ parent::__construct(['value' => $value], ['name' => $name], $line, $tag); |
|
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
public function compile(Twig_Compiler $compiler) |
... | ... |
@@ -661,9 +661,9 @@ method:: |
661 | 661 |
{ |
662 | 662 |
public function getGlobals() |
663 | 663 |
{ |
664 |
- return array( |
|
664 |
+ return [ |
|
665 | 665 |
'text' => new Text(), |
666 |
- ); |
|
666 |
+ ]; |
|
667 | 667 |
} |
668 | 668 |
|
669 | 669 |
// ... |
... | ... |
@@ -679,9 +679,9 @@ method:: |
679 | 679 |
{ |
680 | 680 |
public function getFunctions() |
681 | 681 |
{ |
682 |
- return array( |
|
682 |
+ return [ |
|
683 | 683 |
new Twig_SimpleFunction('lipsum', 'generate_lipsum'), |
684 |
- ); |
|
684 |
+ ]; |
|
685 | 685 |
} |
686 | 686 |
|
687 | 687 |
// ... |
... | ... |
@@ -698,9 +698,9 @@ environment:: |
698 | 698 |
{ |
699 | 699 |
public function getFilters() |
700 | 700 |
{ |
701 |
- return array( |
|
701 |
+ return [ |
|
702 | 702 |
new Twig_SimpleFilter('rot13', 'str_rot13'), |
703 |
- ); |
|
703 |
+ ]; |
|
704 | 704 |
} |
705 | 705 |
|
706 | 706 |
// ... |
... | ... |
@@ -717,7 +717,7 @@ to the Twig environment:: |
717 | 717 |
{ |
718 | 718 |
public function getTokenParsers() |
719 | 719 |
{ |
720 |
- return array(new Project_Set_TokenParser()); |
|
720 |
+ return [new Project_Set_TokenParser()]; |
|
721 | 721 |
} |
722 | 722 |
|
723 | 723 |
// ... |
... | ... |
@@ -737,15 +737,15 @@ The ``getOperators()`` methods lets you add new operators. Here is how to add |
737 | 737 |
{ |
738 | 738 |
public function getOperators() |
739 | 739 |
{ |
740 |
- return array( |
|
741 |
- array( |
|
742 |
- '!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'), |
|
743 |
- ), |
|
744 |
- array( |
|
745 |
- '||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
746 |
- '&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
747 |
- ), |
|
748 |
- ); |
|
740 |
+ return [ |
|
741 |
+ [ |
|
742 |
+ '!' => ['precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'], |
|
743 |
+ ], |
|
744 |
+ [ |
|
745 |
+ '||' => ['precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT], |
|
746 |
+ '&&' => ['precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT], |
|
747 |
+ ], |
|
748 |
+ ]; |
|
749 | 749 |
} |
750 | 750 |
|
751 | 751 |
// ... |
... | ... |
@@ -760,9 +760,9 @@ The ``getTests()`` method lets you add new test functions:: |
760 | 760 |
{ |
761 | 761 |
public function getTests() |
762 | 762 |
{ |
763 |
- return array( |
|
763 |
+ return [ |
|
764 | 764 |
new Twig_SimpleTest('even', 'twig_test_even'), |
765 |
- ); |
|
765 |
+ ]; |
|
766 | 766 |
} |
767 | 767 |
|
768 | 768 |
// ... |
... | ... |
@@ -796,9 +796,9 @@ The simplest way to use methods is to define them on the extension itself:: |
796 | 796 |
|
797 | 797 |
public function getFunctions() |
798 | 798 |
{ |
799 |
- return array( |
|
800 |
- new Twig_SimpleFunction('rot13', array($this, 'rot13')), |
|
801 |
- ); |
|
799 |
+ return [ |
|
800 |
+ new Twig_SimpleFunction('rot13', [$this, 'rot13']), |
|
801 |
+ ]; |
|
802 | 802 |
} |
803 | 803 |
|
804 | 804 |
public function rot13($value) |
... | ... |
@@ -860,11 +860,11 @@ It is now possible to move the runtime logic to a new |
860 | 860 |
{ |
861 | 861 |
public function getFunctions() |
862 | 862 |
{ |
863 |
- return array( |
|
864 |
- new Twig_SimpleFunction('rot13', array('Project_Twig_RuntimeExtension', 'rot13')), |
|
863 |
+ return [ |
|
864 |
+ new Twig_SimpleFunction('rot13', ['Project_Twig_RuntimeExtension', 'rot13']), |
|
865 | 865 |
// or |
866 | 866 |
new Twig_SimpleFunction('rot13', 'Project_Twig_RuntimeExtension::rot13'), |
867 |
- ); |
|
867 |
+ ]; |
|
868 | 868 |
} |
869 | 869 |
} |
870 | 870 |
|
... | ... |
@@ -879,9 +879,9 @@ possible** (order matters):: |
879 | 879 |
{ |
880 | 880 |
public function getFilters() |
881 | 881 |
{ |
882 |
- return array( |
|
883 |
- new Twig_SimpleFilter('date', array($this, 'dateFilter')), |
|
884 |
- ); |
|
882 |
+ return [ |
|
883 |
+ new Twig_SimpleFilter('date', [$this, 'dateFilter']), |
|
884 |
+ ]; |
|
885 | 885 |
} |
886 | 886 |
|
887 | 887 |
public function dateFilter($timestamp, $format = 'F j, Y H:i') |
... | ... |
@@ -938,10 +938,10 @@ The ``IntegrationTest.php`` file should look like this:: |
938 | 938 |
{ |
939 | 939 |
public function getExtensions() |
940 | 940 |
{ |
941 |
- return array( |
|
941 |
+ return [ |
|
942 | 942 |
new Project_Twig_Extension1(), |
943 | 943 |
new Project_Twig_Extension2(), |
944 |
- ); |
|
944 |
+ ]; |
|
945 | 945 |
} |
946 | 946 |
|
947 | 947 |
public function getFixturesDir() |
... | ... |
@@ -227,7 +227,7 @@ The ``Twig_Filter`` classes take options as their last argument. For instance, |
227 | 227 |
if you want access to the current environment instance in your filter, set the |
228 | 228 |
``needs_environment`` option to ``true``:: |
229 | 229 |
|
230 |
- $filter = new Twig_Filter_Function('str_rot13', array('needs_environment' => true)); |
|
230 |
+ $filter = new Twig_Filter_Function('str_rot13', ['needs_environment' => true]); |
|
231 | 231 |
|
232 | 232 |
Twig will then pass the current environment as the first argument to the |
233 | 233 |
filter call:: |
... | ... |
@@ -248,14 +248,14 @@ before printing. If your filter acts as an escaper (or explicitly outputs HTML |
248 | 248 |
or JavaScript code), you will want the raw output to be printed. In such a |
249 | 249 |
case, set the ``is_safe`` option:: |
250 | 250 |
|
251 |
- $filter = new Twig_Filter_Function('nl2br', array('is_safe' => array('html'))); |
|
251 |
+ $filter = new Twig_Filter_Function('nl2br', ['is_safe' => ['html']); |
|
252 | 252 |
|
253 | 253 |
Some filters may need to work on input that is already escaped or safe, for |
254 | 254 |
example when adding (safe) HTML tags to originally unsafe output. In such a |
255 | 255 |
case, set the ``pre_escape`` option to escape the input data before it is run |
256 | 256 |
through your filter:: |
257 | 257 |
|
258 |
- $filter = new Twig_Filter_Function('somefilter', array('pre_escape' => 'html', 'is_safe' => array('html'))); |
|
258 |
+ $filter = new Twig_Filter_Function('somefilter', ['pre_escape' => 'html', 'is_safe' => ['html']]); |
|
259 | 259 |
|
260 | 260 |
Dynamic Filters |
261 | 261 |
~~~~~~~~~~~~~~~ |
... | ... |
@@ -465,7 +465,7 @@ The ``Project_Set_Node`` class itself is rather simple:: |
465 | 465 |
{ |
466 | 466 |
public function __construct($name, Twig_Node_Expression $value, $lineno, $tag = null) |
467 | 467 |
{ |
468 |
- parent::__construct(array('value' => $value), array('name' => $name), $lineno, $tag); |
|
468 |
+ parent::__construct(['value' => $value], ['name' => $name], $lineno, $tag); |
|
469 | 469 |
} |
470 | 470 |
|
471 | 471 |
public function compile(Twig_Compiler $compiler) |
... | ... |
@@ -648,9 +648,9 @@ method:: |
648 | 648 |
{ |
649 | 649 |
public function getGlobals() |
650 | 650 |
{ |
651 |
- return array( |
|
651 |
+ return [ |
|
652 | 652 |
'text' => new Text(), |
653 |
- ); |
|
653 |
+ ]; |
|
654 | 654 |
} |
655 | 655 |
|
656 | 656 |
// ... |
... | ... |
@@ -666,9 +666,9 @@ method:: |
666 | 666 |
{ |
667 | 667 |
public function getFunctions() |
668 | 668 |
{ |
669 |
- return array( |
|
669 |
+ return [ |
|
670 | 670 |
'lipsum' => new Twig_Function_Function('generate_lipsum'), |
671 |
- ); |
|
671 |
+ ]; |
|
672 | 672 |
} |
673 | 673 |
|
674 | 674 |
// ... |
... | ... |
@@ -685,9 +685,9 @@ environment:: |
685 | 685 |
{ |
686 | 686 |
public function getFilters() |
687 | 687 |
{ |
688 |
- return array( |
|
688 |
+ return [ |
|
689 | 689 |
'rot13' => new Twig_Filter_Function('str_rot13'), |
690 |
- ); |
|
690 |
+ ]; |
|
691 | 691 |
} |
692 | 692 |
|
693 | 693 |
// ... |
... | ... |
@@ -709,9 +709,9 @@ when defining a filter to use a method:: |
709 | 709 |
{ |
710 | 710 |
public function getFilters() |
711 | 711 |
{ |
712 |
- return array( |
|
712 |
+ return [ |
|
713 | 713 |
'rot13' => new Twig_Filter_Method($this, 'rot13Filter'), |
714 |
- ); |
|
714 |
+ ]; |
|
715 | 715 |
} |
716 | 716 |
|
717 | 717 |
public function rot13Filter($string) |
... | ... |
@@ -741,10 +741,10 @@ want to override:: |
741 | 741 |
{ |
742 | 742 |
public function getFilters() |
743 | 743 |
{ |
744 |
- return array( |
|
744 |
+ return [ |
|
745 | 745 |
'date' => new Twig_Filter_Method($this, 'dateFilter'), |
746 | 746 |
// ... |
747 |
- ); |
|
747 |
+ ]; |
|
748 | 748 |
} |
749 | 749 |
|
750 | 750 |
public function dateFilter($timestamp, $format = 'F j, Y H:i') |
... | ... |
@@ -776,7 +776,7 @@ to the Twig environment:: |
776 | 776 |
{ |
777 | 777 |
public function getTokenParsers() |
778 | 778 |
{ |
779 |
- return array(new Project_Set_TokenParser()); |
|
779 |
+ return [new Project_Set_TokenParser()]; |
|
780 | 780 |
} |
781 | 781 |
|
782 | 782 |
// ... |
... | ... |
@@ -796,15 +796,15 @@ The ``getOperators()`` methods allows to add new operators. Here is how to add |
796 | 796 |
{ |
797 | 797 |
public function getOperators() |
798 | 798 |
{ |
799 |
- return array( |
|
800 |
- array( |
|
801 |
- '!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'), |
|
799 |
+ return [ |
|
800 |
+ [ |
|
801 |
+ '!' => ['precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'], |
|
802 | 802 |
), |
803 |
- array( |
|
804 |
- '||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
805 |
- '&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT), |
|
806 |
- ), |
|
807 |
- ); |
|
803 |
+ [ |
|
804 |
+ '||' => ['precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT], |
|
805 |
+ '&&' => ['precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT], |
|
806 |
+ ], |
|
807 |
+ ]; |
|
808 | 808 |
} |
809 | 809 |
|
810 | 810 |
// ... |
... | ... |
@@ -819,9 +819,9 @@ The ``getTests()`` methods allows to add new test functions:: |
819 | 819 |
{ |
820 | 820 |
public function getTests() |
821 | 821 |
{ |
822 |
- return array( |
|
822 |
+ return [ |
|
823 | 823 |
'even' => new Twig_Test_Function('twig_test_even'), |
824 |
- ); |
|
824 |
+ ]; |
|
825 | 825 |
} |
826 | 826 |
|
827 | 827 |
// ... |
... | ... |
@@ -857,10 +857,10 @@ The ``IntegrationTest.php`` file should look like this:: |
857 | 857 |
{ |
858 | 858 |
public function getExtensions() |
859 | 859 |
{ |
860 |
- return array( |
|
860 |
+ return [ |
|
861 | 861 |
new Project_Twig_Extension1(), |
862 | 862 |
new Project_Twig_Extension2(), |
863 |
- ); |
|
863 |
+ ]; |
|
864 | 864 |
} |
865 | 865 |
|
866 | 866 |
public function getFixturesDir() |
... | ... |
@@ -25,9 +25,9 @@ looks roughly like this:: |
25 | 25 |
Twig_Autoloader::register(); |
26 | 26 |
|
27 | 27 |
$loader = new Twig_Loader_Filesystem('/path/to/templates'); |
28 |
- $twig = new Twig_Environment($loader, array( |
|
28 |
+ $twig = new Twig_Environment($loader, [ |
|
29 | 29 |
'cache' => '/path/to/compilation_cache', |
30 |
- )); |
|
30 |
+ ]); |
|
31 | 31 |
|
32 | 32 |
This will create a template environment with the default settings and a loader |
33 | 33 |
that looks up the templates in the ``/path/to/templates/`` folder. Different |
... | ... |
@@ -58,7 +58,7 @@ returns a ``Twig_TemplateWrapper`` instance:: |
58 | 58 |
|
59 | 59 |
To render the template with some variables, call the ``render()`` method:: |
60 | 60 |
|
61 |
- echo $template->render(array('the' => 'variables', 'go' => 'here')); |
|
61 |
+ echo $template->render(['the' => 'variables', 'go' => 'here']); |
|
62 | 62 |
|
63 | 63 |
.. note:: |
64 | 64 |
|
... | ... |
@@ -66,7 +66,7 @@ To render the template with some variables, call the ``render()`` method:: |
66 | 66 |
|
67 | 67 |
You can also load and render the template in one fell swoop:: |
68 | 68 |
|
69 |
- echo $twig->render('index.html', array('the' => 'variables', 'go' => 'here')); |
|
69 |
+ echo $twig->render('index.html', ['the' => 'variables', 'go' => 'here']); |
|
70 | 70 |
|
71 | 71 |
.. versionadded:: 1.28 |
72 | 72 |
The possibility to render blocks from the API was added in Twig 1.28. |
... | ... |
@@ -74,7 +74,7 @@ You can also load and render the template in one fell swoop:: |
74 | 74 |
If a template defines blocks, they can be rendered individually via the |
75 | 75 |
``renderBlock()`` call:: |
76 | 76 |
|
77 |
- echo $template->renderBlock('block_name', array('the' => 'variables', 'go' => 'here')); |
|
77 |
+ echo $template->renderBlock('block_name', ['the' => 'variables', 'go' => 'here']); |
|
78 | 78 |
|
79 | 79 |
.. _environment_options: |
80 | 80 |
|
... | ... |
@@ -84,7 +84,7 @@ Environment Options |
84 | 84 |
When creating a new ``Twig_Environment`` instance, you can pass an array of |
85 | 85 |
options as the constructor second argument:: |
86 | 86 |
|
87 |
- $twig = new Twig_Environment($loader, array('debug' => true)); |
|
87 |
+ $twig = new Twig_Environment($loader, ['debug' => true]); |
|
88 | 88 |
|
89 | 89 |
The following options are available: |
90 | 90 |
|
... | ... |
@@ -183,7 +183,7 @@ load them:: |
183 | 183 |
|
184 | 184 |
It can also look for templates in an array of directories:: |
185 | 185 |
|
186 |
- $loader = new Twig_Loader_Filesystem(array($templateDir1, $templateDir2)); |
|
186 |
+ $loader = new Twig_Loader_Filesystem([$templateDir1, $templateDir2]); |
|
187 | 187 |
|
188 | 188 |
With such a configuration, Twig will first look for templates in |
189 | 189 |
``$templateDir1`` and if they do not exist, it will fallback to look for them |
... | ... |
@@ -207,7 +207,7 @@ methods act on the "main" namespace):: |
207 | 207 |
Namespaced templates can be accessed via the special |
208 | 208 |
``@namespace_name/template_path`` notation:: |
209 | 209 |
|
210 |
- $twig->render('@admin/index.html', array()); |
|
210 |
+ $twig->render('@admin/index.html', []); |
|
211 | 211 |
|
212 | 212 |
``Twig_Loader_Filesystem`` support absolute and relative paths. Using relative |
213 | 213 |
paths is preferred as it makes the cache keys independent of the project root |
... | ... |
@@ -227,12 +227,12 @@ the directory might be different from the one used on production servers):: |
227 | 227 |
``Twig_Loader_Array`` loads a template from a PHP array. It's passed an array |
228 | 228 |
of strings bound to template names:: |
229 | 229 |
|
230 |
- $loader = new Twig_Loader_Array(array( |
|
230 |
+ $loader = new Twig_Loader_Array([ |
|
231 | 231 |
'index.html' => 'Hello {{ name }}!', |
232 |
- )); |
|
232 |
+ ]); |
|
233 | 233 |
$twig = new Twig_Environment($loader); |
234 | 234 |
|
235 |
- echo $twig->render('index.html', array('name' => 'Fabien')); |
|
235 |
+ echo $twig->render('index.html', ['name' => 'Fabien']); |
|
236 | 236 |
|
237 | 237 |
This loader is very useful for unit testing. It can also be used for small |
238 | 238 |
projects where storing all templates in a single PHP file might make sense. |
... | ... |
@@ -250,15 +250,15 @@ projects where storing all templates in a single PHP file might make sense. |
250 | 250 |
|
251 | 251 |
``Twig_Loader_Chain`` delegates the loading of templates to other loaders:: |
252 | 252 |
|
253 |
- $loader1 = new Twig_Loader_Array(array( |
|
253 |
+ $loader1 = new Twig_Loader_Array([ |
|
254 | 254 |
'base.html' => '{% block content %}{% endblock %}', |
255 |
- )); |
|
256 |
- $loader2 = new Twig_Loader_Array(array( |
|
255 |
+ ]); |
|
256 |
+ $loader2 = new Twig_Loader_Array([ |
|
257 | 257 |
'index.html' => '{% extends "base.html" %}{% block content %}Hello {{ name }}{% endblock %}', |
258 | 258 |
'base.html' => 'Will never be loaded', |
259 |
- )); |
|
259 |
+ ]); |
|
260 | 260 |
|
261 |
- $loader = new Twig_Loader_Chain(array($loader1, $loader2)); |
|
261 |
+ $loader = new Twig_Loader_Chain([$loader1, $loader2]); |
|
262 | 262 |
|
263 | 263 |
$twig = new Twig_Environment($loader); |
264 | 264 |
|
... | ... |
@@ -476,15 +476,15 @@ by a policy instance. By default, Twig comes with one policy class: |
476 | 476 |
``Twig_Sandbox_SecurityPolicy``. This class allows you to white-list some |
477 | 477 |
tags, filters, properties, and methods:: |
478 | 478 |
|
479 |
- $tags = array('if'); |
|
480 |
- $filters = array('upper'); |
|
481 |
- $methods = array( |
|
482 |
- 'Article' => array('getTitle', 'getBody'), |
|
483 |
- ); |
|
484 |
- $properties = array( |
|
485 |
- 'Article' => array('title', 'body'), |
|
486 |
- ); |
|
487 |
- $functions = array('range'); |
|
479 |
+ $tags = ['if']; |
|
480 |
+ $filters = ['upper']; |
|
481 |
+ $methods = [ |
|
482 |
+ 'Article' => ['getTitle', 'getBody'], |
|
483 |
+ ]; |
|
484 |
+ $properties = [ |
|
485 |
+ 'Article' => ['title', 'body'], |
|
486 |
+ ]; |
|
487 |
+ $functions = ['range']; |
|
488 | 488 |
$policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions); |
489 | 489 |
|
490 | 490 |
With the previous configuration, the security policy will only allow usage of |
... | ... |
@@ -18,10 +18,10 @@ introspecting its variables: |
18 | 18 |
``Twig_Extension_Debug`` extension explicitly when creating your Twig |
19 | 19 |
environment:: |
20 | 20 |
|
21 |
- $twig = new Twig_Environment($loader, array( |
|
21 |
+ $twig = new Twig_Environment($loader, [ |
|
22 | 22 |
'debug' => true, |
23 | 23 |
// ... |
24 |
- )); |
|
24 |
+ ]); |
|
25 | 25 |
$twig->addExtension(new Twig_Extension_Debug()); |
26 | 26 |
|
27 | 27 |
Even when enabled, the ``dump`` function won't display anything if the |
... | ... |
@@ -48,7 +48,7 @@ And if the expression evaluates to a ``Twig_Template`` or a |
48 | 48 |
// as of Twig 1.28 |
49 | 49 |
$template = $twig->load('some_template.twig'); |
50 | 50 |
|
51 |
- $twig->display('template.twig', array('template' => $template)); |
|
51 |
+ $twig->display('template.twig', ['template' => $template]); |
|
52 | 52 |
|
53 | 53 |
When you set the ``ignore_missing`` flag, Twig will return an empty string if |
54 | 54 |
the template does not exist: |
... | ... |
@@ -124,7 +124,7 @@ using):: |
124 | 124 |
/* Hello {{ name }} */ |
125 | 125 |
class __TwigTemplate_1121b6f109fe93ebe8c6e22e3712bceb extends Twig_Template |
126 | 126 |
{ |
127 |
- protected function doDisplay(array $context, array $blocks = array()) |
|
127 |
+ protected function doDisplay(array $context, array $blocks = []) |
|
128 | 128 |
{ |
129 | 129 |
// line 1 |
130 | 130 |
echo "Hello "; |
... | ... |
@@ -55,12 +55,12 @@ This section gives you a brief introduction to the PHP API for Twig. |
55 | 55 |
|
56 | 56 |
require_once '/path/to/vendor/autoload.php'; |
57 | 57 |
|
58 |
- $loader = new Twig_Loader_Array(array( |
|
58 |
+ $loader = new Twig_Loader_Array([ |
|
59 | 59 |
'index' => 'Hello {{ name }}!', |
60 |
- )); |
|
60 |
+ ]); |
|
61 | 61 |
$twig = new Twig_Environment($loader); |
62 | 62 |
|
63 |
- echo $twig->render('index', array('name' => 'Fabien')); |
|
63 |
+ echo $twig->render('index', ['name' => 'Fabien']); |
|
64 | 64 |
|
65 | 65 |
Twig uses a loader (``Twig_Loader_Array``) to locate templates, and an |
66 | 66 |
environment (``Twig_Environment``) to store the configuration. |
... | ... |
@@ -72,11 +72,11 @@ As templates are generally stored on the filesystem, Twig also comes with a |
72 | 72 |
filesystem loader:: |
73 | 73 |
|
74 | 74 |
$loader = new Twig_Loader_Filesystem('/path/to/templates'); |
75 |
- $twig = new Twig_Environment($loader, array( |
|
75 |
+ $twig = new Twig_Environment($loader, [ |
|
76 | 76 |
'cache' => '/path/to/compilation_cache', |
77 |
- )); |
|
77 |
+ ]); |
|
78 | 78 |
|
79 |
- echo $twig->render('index.html', array('name' => 'Fabien')); |
|
79 |
+ echo $twig->render('index.html', ['name' => 'Fabien']); |
|
80 | 80 |
|
81 | 81 |
.. tip:: |
82 | 82 |
|
... | ... |
@@ -38,7 +38,7 @@ However, this code won't find all deprecations (like using deprecated some Twig |
38 | 38 |
classes). To catch all notices, register a custom error handler like the one |
39 | 39 |
below:: |
40 | 40 |
|
41 |
- $deprecations = array(); |
|
41 |
+ $deprecations = []; |
|
42 | 42 |
set_error_handler(function ($type, $msg) use (&$deprecations) { |
43 | 43 |
if (E_USER_DEPRECATED === $type) { |
44 | 44 |
$deprecations[] = $msg; |
... | ... |
@@ -163,37 +163,37 @@ To change the block delimiters, you need to create your own lexer object:: |
163 | 163 |
|
164 | 164 |
$twig = new Twig_Environment(); |
165 | 165 |
|
166 |
- $lexer = new Twig_Lexer($twig, array( |
|
167 |
- 'tag_comment' => array('{#', '#}'), |
|
168 |
- 'tag_block' => array('{%', '%}'), |
|
169 |
- 'tag_variable' => array('{{', '}}'), |
|
170 |
- 'interpolation' => array('#{', '}'), |
|
171 |
- )); |
|
166 |
+ $lexer = new Twig_Lexer($twig, [ |
|
167 |
+ 'tag_comment' => ['{#', '#}'], |
|
168 |
+ 'tag_block' => ['{%', '%}'], |
|
169 |
+ 'tag_variable' => ['{{', '}}'], |
|
170 |
+ 'interpolation' => ['#{', '}'], |
|
171 |
+ ]); |
|
172 | 172 |
$twig->setLexer($lexer); |
173 | 173 |
|
174 | 174 |
Here are some configuration example that simulates some other template engines |
175 | 175 |
syntax:: |
176 | 176 |
|
177 | 177 |
// Ruby erb syntax |
178 |
- $lexer = new Twig_Lexer($twig, array( |
|
179 |
- 'tag_comment' => array('<%#', '%>'), |
|
180 |
- 'tag_block' => array('<%', '%>'), |
|
181 |
- 'tag_variable' => array('<%=', '%>'), |
|
182 |
- )); |
|
178 |
+ $lexer = new Twig_Lexer($twig, [ |
|
179 |
+ 'tag_comment' => ['<%#', '%>'], |
|
180 |
+ 'tag_block' => ['<%', '%>'], |
|
181 |
+ 'tag_variable' => ['<%=', '%>'], |
|
182 |
+ ]); |
|
183 | 183 |
|
184 | 184 |
// SGML Comment Syntax |
185 |
- $lexer = new Twig_Lexer($twig, array( |
|
186 |
- 'tag_comment' => array('<!--#', '-->'), |
|
187 |
- 'tag_block' => array('<!--', '-->'), |
|
188 |
- 'tag_variable' => array('${', '}'), |
|
189 |
- )); |
|
185 |
+ $lexer = new Twig_Lexer($twig, [ |
|
186 |
+ 'tag_comment' => ['<!--#', '-->'], |
|
187 |
+ 'tag_block' => ['<!--', '-->'], |
|
188 |
+ 'tag_variable' => ['${', '}'], |
|
189 |
+ ]); |
|
190 | 190 |
|
191 | 191 |
// Smarty like |
192 |
- $lexer = new Twig_Lexer($twig, array( |
|
193 |
- 'tag_comment' => array('{*', '*}'), |
|
194 |
- 'tag_block' => array('{', '}'), |
|
195 |
- 'tag_variable' => array('{$', '}'), |
|
196 |
- )); |
|
192 |
+ $lexer = new Twig_Lexer($twig, [ |
|
193 |
+ 'tag_comment' => ['{*', '*}'], |
|
194 |
+ 'tag_block' => ['{', '}'], |
|
195 |
+ 'tag_variable' => ['{$', '}'], |
|
196 |
+ ]); |
|
197 | 197 |
|
198 | 198 |
Using dynamic Object Properties |
199 | 199 |
------------------------------- |
... | ... |
@@ -233,12 +233,12 @@ Sometimes, when using nested loops, you need to access the parent context. The |
233 | 233 |
parent context is always accessible via the ``loop.parent`` variable. For |
234 | 234 |
instance, if you have the following template data:: |
235 | 235 |
|
236 |
- $data = array( |
|
237 |
- 'topics' => array( |
|
238 |
- 'topic1' => array('Message 1 of topic 1', 'Message 2 of topic 1'), |
|
239 |
- 'topic2' => array('Message 1 of topic 2', 'Message 2 of topic 2'), |
|
240 |
- ), |
|
241 |
- ); |
|
236 |
+ $data = [ |
|
237 |
+ 'topics' => [ |
|
238 |
+ 'topic1' => ['Message 1 of topic 1', 'Message 2 of topic 1'], |
|
239 |
+ 'topic2' => ['Message 1 of topic 2', 'Message 2 of topic 2'], |
|
240 |
+ ], |
|
241 |
+ ]; |
|
242 | 242 |
|
243 | 243 |
And the following template to display all messages in all topics: |
244 | 244 |
|
... | ... |
@@ -345,10 +345,10 @@ cache won't update the cache. |
345 | 345 |
|
346 | 346 |
To get around this, force Twig to invalidate the bytecode cache:: |
347 | 347 |
|
348 |
- $twig = new Twig_Environment($loader, array( |
|
348 |
+ $twig = new Twig_Environment($loader, [ |
|
349 | 349 |
'cache' => new Twig_Cache_Filesystem('/some/cache/path', Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION), |
350 | 350 |
// ... |
351 |
- )); |
|
351 |
+ ]); |
|
352 | 352 |
|
353 | 353 |
.. note:: |
354 | 354 |
|
... | ... |
@@ -378,13 +378,13 @@ around, you probably want to reset it when visiting a new template. |
378 | 378 |
|
379 | 379 |
This can be easily achieved with the following code:: |
380 | 380 |
|
381 |
- protected $someTemplateState = array(); |
|
381 |
+ protected $someTemplateState = []; |
|
382 | 382 |
|
383 | 383 |
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) |
384 | 384 |
{ |
385 | 385 |
if ($node instanceof Twig_Node_Module) { |
386 | 386 |
// reset the state as we are entering a new template |
387 |
- $this->someTemplateState = array(); |
|
387 |
+ $this->someTemplateState = []; |
|
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
// ... |
... | ... |
@@ -468,7 +468,7 @@ Now, let's define a loader able to use this database:: |
468 | 468 |
protected function getValue($column, $name) |
469 | 469 |
{ |
470 | 470 |
$sth = $this->dbh->prepare('SELECT '.$column.' FROM templates WHERE name = :name'); |
471 |
- $sth->execute(array(':name' => (string) $name)); |
|
471 |
+ $sth->execute([':name' => (string) $name]); |
|
472 | 472 |
|
473 | 473 |
return $sth->fetchColumn(); |
474 | 474 |
} |
... | ... |
@@ -479,7 +479,7 @@ Finally, here is an example on how you can use it:: |
479 | 479 |
$loader = new DatabaseTwigLoader($dbh); |
480 | 480 |
$twig = new Twig_Environment($loader); |
481 | 481 |
|
482 |
- echo $twig->render('index.twig', array('name' => 'Fabien')); |
|
482 |
+ echo $twig->render('index.twig', ['name' => 'Fabien']); |
|
483 | 483 |
|
484 | 484 |
Using different Template Sources |
485 | 485 |
-------------------------------- |
... | ... |
@@ -496,14 +496,14 @@ filesystem, or any other loader for that matter: the template name should be a |
496 | 496 |
logical name, and not the path from the filesystem:: |
497 | 497 |
|
498 | 498 |
$loader1 = new DatabaseTwigLoader($dbh); |
499 |
- $loader2 = new Twig_Loader_Array(array( |
|
499 |
+ $loader2 = new Twig_Loader_Array([ |
|
500 | 500 |
'base.twig' => '{% block content %}{% endblock %}', |
501 |
- )); |
|
502 |
- $loader = new Twig_Loader_Chain(array($loader1, $loader2)); |
|
501 |
+ ]); |
|
502 |
+ $loader = new Twig_Loader_Chain([$loader1, $loader2]); |
|
503 | 503 |
|
504 | 504 |
$twig = new Twig_Environment($loader); |
505 | 505 |
|
506 |
- echo $twig->render('index.twig', array('name' => 'Fabien')); |
|
506 |
+ echo $twig->render('index.twig', ['name' => 'Fabien']); |
|
507 | 507 |
|
508 | 508 |
Now that the ``base.twig`` templates is defined in an array loader, you can |
509 | 509 |
remove it from the database, and everything else will still work as before. |
... | ... |
@@ -523,7 +523,7 @@ From PHP, it's also possible to load a template stored in a string via |
523 | 523 |
``Twig_Environment::createTemplate()`` (available as of Twig 1.18):: |
524 | 524 |
|
525 | 525 |
$template = $twig->createTemplate('hello {{ name }}'); |
526 |
- echo $template->render(array('name' => 'Fabien')); |
|
526 |
+ echo $template->render(['name' => 'Fabien']); |
|
527 | 527 |
|
528 | 528 |
.. note:: |
529 | 529 |
|
... | ... |
@@ -561,8 +561,8 @@ include in your templates: |
561 | 561 |
|
562 | 562 |
.. code-block:: php |
563 | 563 |
|
564 |
- $env->setLexer(new Twig_Lexer($env, array( |
|
565 |
- 'tag_variable' => array('{[', ']}'), |
|
566 |
- ))); |
|
564 |
+ $env->setLexer(new Twig_Lexer($env, [ |
|
565 |
+ 'tag_variable' => ['{[', ']}'], |
|
566 |
+ ])); |
|
567 | 567 |
|
568 | 568 |
.. _callback: https://secure.php.net/manual/en/function.is-callable.php |
... | ... |
@@ -164,7 +164,7 @@ instance, Twig will use it as the parent template:: |
164 | 164 |
// as of Twig 1.28 |
165 | 165 |
$layout = $twig->load('some_layout_template.twig'); |
166 | 166 |
|
167 |
- $twig->display('template.twig', array('layout' => $layout)); |
|
167 |
+ $twig->display('template.twig', ['layout' => $layout]); |
|
168 | 168 |
|
169 | 169 |
.. versionadded:: 1.2 |
170 | 170 |
The possibility to pass an array of templates has been added in Twig 1.2. |
... | ... |
@@ -61,7 +61,7 @@ And if the expression evaluates to a ``Twig_Template`` or a |
61 | 61 |
// as of Twig 1.28 |
62 | 62 |
$template = $twig->load('some_template.twig'); |
63 | 63 |
|
64 |
- $twig->display('template.twig', array('template' => $template)); |
|
64 |
+ $twig->display('template.twig', ['template' => $template]); |
|
65 | 65 |
|
66 | 66 |
.. versionadded:: 1.2 |
67 | 67 |
The ``ignore missing`` feature has been added in Twig 1.2. |