mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 17:36:53 +00:00
enabled short array notation in docs
This commit is contained in:
+42
-42
@@ -137,13 +137,13 @@ Creating a filter is as simple as associating a name with a PHP callable::
|
||||
$filter = new Twig_SimpleFilter('rot13', 'str_rot13');
|
||||
|
||||
// or a class static method
|
||||
$filter = new Twig_SimpleFilter('rot13', array('SomeClass', 'rot13Filter'));
|
||||
$filter = new Twig_SimpleFilter('rot13', ['SomeClass', 'rot13Filter']);
|
||||
$filter = new Twig_SimpleFilter('rot13', 'SomeClass::rot13Filter');
|
||||
|
||||
// or a class method
|
||||
$filter = new Twig_SimpleFilter('rot13', array($this, 'rot13Filter'));
|
||||
$filter = new Twig_SimpleFilter('rot13', [$this, 'rot13Filter']);
|
||||
// the one below needs a runtime implementation (see below for more information)
|
||||
$filter = new Twig_SimpleFilter('rot13', array('SomeClass', 'rot13Filter'));
|
||||
$filter = new Twig_SimpleFilter('rot13', ['SomeClass', 'rot13Filter']);
|
||||
|
||||
The first argument passed to the ``Twig_SimpleFilter`` constructor is the name
|
||||
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::
|
||||
$charset = $env->getCharset();
|
||||
|
||||
return str_rot13($string);
|
||||
}, array('needs_environment' => true));
|
||||
}, ['needs_environment' => true]);
|
||||
|
||||
Context-aware Filters
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
@@ -207,11 +207,11 @@ the first argument to the filter call (or the second one if
|
||||
|
||||
$filter = new Twig_SimpleFilter('rot13', function ($context, $string) {
|
||||
// ...
|
||||
}, array('needs_context' => true));
|
||||
}, ['needs_context' => true]);
|
||||
|
||||
$filter = new Twig_SimpleFilter('rot13', function (Twig_Environment $env, $context, $string) {
|
||||
// ...
|
||||
}, array('needs_context' => true, 'needs_environment' => true));
|
||||
}, ['needs_context' => true, 'needs_environment' => true]);
|
||||
|
||||
Automatic Escaping
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
@@ -221,14 +221,14 @@ before printing. If your filter acts as an escaper (or explicitly outputs HTML
|
||||
or JavaScript code), you will want the raw output to be printed. In such a
|
||||
case, set the ``is_safe`` option::
|
||||
|
||||
$filter = new Twig_SimpleFilter('nl2br', 'nl2br', array('is_safe' => array('html')));
|
||||
$filter = new Twig_SimpleFilter('nl2br', 'nl2br', ['is_safe' => ['html']]);
|
||||
|
||||
Some filters may need to work on input that is already escaped or safe, for
|
||||
example when adding (safe) HTML tags to originally unsafe output. In such a
|
||||
case, set the ``pre_escape`` option to escape the input data before it is run
|
||||
through your filter::
|
||||
|
||||
$filter = new Twig_SimpleFilter('somefilter', 'somefilter', array('pre_escape' => 'html', 'is_safe' => array('html')));
|
||||
$filter = new Twig_SimpleFilter('somefilter', 'somefilter', ['pre_escape' => 'html', 'is_safe' => ['html']]);
|
||||
|
||||
Variadic Filters
|
||||
~~~~~~~~~~~~~~~~
|
||||
@@ -240,9 +240,9 @@ When a filter should accept an arbitrary number of arguments, set the
|
||||
``is_variadic`` option to ``true``; Twig will pass the extra arguments as the
|
||||
last argument to the filter call as an array::
|
||||
|
||||
$filter = new Twig_SimpleFilter('thumbnail', function ($file, array $options = array()) {
|
||||
$filter = new Twig_SimpleFilter('thumbnail', function ($file, array $options = []) {
|
||||
// ...
|
||||
}, array('is_variadic' => true));
|
||||
}, ['is_variadic' => true]);
|
||||
|
||||
Be warned that named arguments passed to a variadic filter cannot be checked
|
||||
for validity as they will automatically end up in the option array.
|
||||
@@ -285,7 +285,7 @@ deprecated one when that makes sense::
|
||||
|
||||
$filter = new Twig_SimpleFilter('obsolete', function () {
|
||||
// ...
|
||||
}, array('deprecated' => true, 'alternative' => 'new_one'));
|
||||
}, ['deprecated' => true, 'alternative' => 'new_one']);
|
||||
|
||||
When a filter is deprecated, Twig emits a deprecation notice when compiling a
|
||||
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::
|
||||
$test = new Twig_SimpleTest(
|
||||
'odd',
|
||||
null,
|
||||
array('node_class' => 'Twig_Node_Expression_Test_Odd'));
|
||||
['node_class' => 'Twig_Node_Expression_Test_Odd']);
|
||||
$twig->addTest($test);
|
||||
|
||||
class Twig_Node_Expression_Test_Odd extends Twig_Node_Expression_Test
|
||||
@@ -486,7 +486,7 @@ The ``Project_Set_Node`` class itself is rather simple::
|
||||
{
|
||||
public function __construct($name, Twig_Node_Expression $value, $line, $tag = null)
|
||||
{
|
||||
parent::__construct(array('value' => $value), array('name' => $name), $line, $tag);
|
||||
parent::__construct(['value' => $value], ['name' => $name], $line, $tag);
|
||||
}
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
@@ -661,9 +661,9 @@ method::
|
||||
{
|
||||
public function getGlobals()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'text' => new Text(),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -679,9 +679,9 @@ method::
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
new Twig_SimpleFunction('lipsum', 'generate_lipsum'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -698,9 +698,9 @@ environment::
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
new Twig_SimpleFilter('rot13', 'str_rot13'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -717,7 +717,7 @@ to the Twig environment::
|
||||
{
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array(new Project_Set_TokenParser());
|
||||
return [new Project_Set_TokenParser()];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -737,15 +737,15 @@ The ``getOperators()`` methods lets you add new operators. Here is how to add
|
||||
{
|
||||
public function getOperators()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
|
||||
),
|
||||
array(
|
||||
'||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
|
||||
'&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
|
||||
),
|
||||
);
|
||||
return [
|
||||
[
|
||||
'!' => ['precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'],
|
||||
],
|
||||
[
|
||||
'||' => ['precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT],
|
||||
'&&' => ['precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -760,9 +760,9 @@ The ``getTests()`` method lets you add new test functions::
|
||||
{
|
||||
public function getTests()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
new Twig_SimpleTest('even', 'twig_test_even'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -796,9 +796,9 @@ The simplest way to use methods is to define them on the extension itself::
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
new Twig_SimpleFunction('rot13', array($this, 'rot13')),
|
||||
);
|
||||
return [
|
||||
new Twig_SimpleFunction('rot13', [$this, 'rot13']),
|
||||
];
|
||||
}
|
||||
|
||||
public function rot13($value)
|
||||
@@ -860,11 +860,11 @@ It is now possible to move the runtime logic to a new
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
new Twig_SimpleFunction('rot13', array('Project_Twig_RuntimeExtension', 'rot13')),
|
||||
return [
|
||||
new Twig_SimpleFunction('rot13', ['Project_Twig_RuntimeExtension', 'rot13']),
|
||||
// or
|
||||
new Twig_SimpleFunction('rot13', 'Project_Twig_RuntimeExtension::rot13'),
|
||||
);
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -879,9 +879,9 @@ possible** (order matters)::
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new Twig_SimpleFilter('date', array($this, 'dateFilter')),
|
||||
);
|
||||
return [
|
||||
new Twig_SimpleFilter('date', [$this, 'dateFilter']),
|
||||
];
|
||||
}
|
||||
|
||||
public function dateFilter($timestamp, $format = 'F j, Y H:i')
|
||||
@@ -938,10 +938,10 @@ The ``IntegrationTest.php`` file should look like this::
|
||||
{
|
||||
public function getExtensions()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
new Project_Twig_Extension1(),
|
||||
new Project_Twig_Extension2(),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
public function getFixturesDir()
|
||||
|
||||
+27
-27
@@ -227,7 +227,7 @@ The ``Twig_Filter`` classes take options as their last argument. For instance,
|
||||
if you want access to the current environment instance in your filter, set the
|
||||
``needs_environment`` option to ``true``::
|
||||
|
||||
$filter = new Twig_Filter_Function('str_rot13', array('needs_environment' => true));
|
||||
$filter = new Twig_Filter_Function('str_rot13', ['needs_environment' => true]);
|
||||
|
||||
Twig will then pass the current environment as the first argument to the
|
||||
filter call::
|
||||
@@ -248,14 +248,14 @@ before printing. If your filter acts as an escaper (or explicitly outputs HTML
|
||||
or JavaScript code), you will want the raw output to be printed. In such a
|
||||
case, set the ``is_safe`` option::
|
||||
|
||||
$filter = new Twig_Filter_Function('nl2br', array('is_safe' => array('html')));
|
||||
$filter = new Twig_Filter_Function('nl2br', ['is_safe' => ['html']);
|
||||
|
||||
Some filters may need to work on input that is already escaped or safe, for
|
||||
example when adding (safe) HTML tags to originally unsafe output. In such a
|
||||
case, set the ``pre_escape`` option to escape the input data before it is run
|
||||
through your filter::
|
||||
|
||||
$filter = new Twig_Filter_Function('somefilter', array('pre_escape' => 'html', 'is_safe' => array('html')));
|
||||
$filter = new Twig_Filter_Function('somefilter', ['pre_escape' => 'html', 'is_safe' => ['html']]);
|
||||
|
||||
Dynamic Filters
|
||||
~~~~~~~~~~~~~~~
|
||||
@@ -465,7 +465,7 @@ The ``Project_Set_Node`` class itself is rather simple::
|
||||
{
|
||||
public function __construct($name, Twig_Node_Expression $value, $lineno, $tag = null)
|
||||
{
|
||||
parent::__construct(array('value' => $value), array('name' => $name), $lineno, $tag);
|
||||
parent::__construct(['value' => $value], ['name' => $name], $lineno, $tag);
|
||||
}
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
@@ -648,9 +648,9 @@ method::
|
||||
{
|
||||
public function getGlobals()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'text' => new Text(),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -666,9 +666,9 @@ method::
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'lipsum' => new Twig_Function_Function('generate_lipsum'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -685,9 +685,9 @@ environment::
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'rot13' => new Twig_Filter_Function('str_rot13'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -709,9 +709,9 @@ when defining a filter to use a method::
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'rot13' => new Twig_Filter_Method($this, 'rot13Filter'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
public function rot13Filter($string)
|
||||
@@ -741,10 +741,10 @@ want to override::
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'date' => new Twig_Filter_Method($this, 'dateFilter'),
|
||||
// ...
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
public function dateFilter($timestamp, $format = 'F j, Y H:i')
|
||||
@@ -776,7 +776,7 @@ to the Twig environment::
|
||||
{
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array(new Project_Set_TokenParser());
|
||||
return [new Project_Set_TokenParser()];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -796,15 +796,15 @@ The ``getOperators()`` methods allows to add new operators. Here is how to add
|
||||
{
|
||||
public function getOperators()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
|
||||
return [
|
||||
[
|
||||
'!' => ['precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'],
|
||||
),
|
||||
array(
|
||||
'||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
|
||||
'&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
|
||||
),
|
||||
);
|
||||
[
|
||||
'||' => ['precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT],
|
||||
'&&' => ['precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -819,9 +819,9 @@ The ``getTests()`` methods allows to add new test functions::
|
||||
{
|
||||
public function getTests()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
'even' => new Twig_Test_Function('twig_test_even'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -857,10 +857,10 @@ The ``IntegrationTest.php`` file should look like this::
|
||||
{
|
||||
public function getExtensions()
|
||||
{
|
||||
return array(
|
||||
return [
|
||||
new Project_Twig_Extension1(),
|
||||
new Project_Twig_Extension2(),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
public function getFixturesDir()
|
||||
|
||||
+25
-25
@@ -25,9 +25,9 @@ looks roughly like this::
|
||||
Twig_Autoloader::register();
|
||||
|
||||
$loader = new Twig_Loader_Filesystem('/path/to/templates');
|
||||
$twig = new Twig_Environment($loader, array(
|
||||
$twig = new Twig_Environment($loader, [
|
||||
'cache' => '/path/to/compilation_cache',
|
||||
));
|
||||
]);
|
||||
|
||||
This will create a template environment with the default settings and a loader
|
||||
that looks up the templates in the ``/path/to/templates/`` folder. Different
|
||||
@@ -58,7 +58,7 @@ returns a ``Twig_TemplateWrapper`` instance::
|
||||
|
||||
To render the template with some variables, call the ``render()`` method::
|
||||
|
||||
echo $template->render(array('the' => 'variables', 'go' => 'here'));
|
||||
echo $template->render(['the' => 'variables', 'go' => 'here']);
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -66,7 +66,7 @@ To render the template with some variables, call the ``render()`` method::
|
||||
|
||||
You can also load and render the template in one fell swoop::
|
||||
|
||||
echo $twig->render('index.html', array('the' => 'variables', 'go' => 'here'));
|
||||
echo $twig->render('index.html', ['the' => 'variables', 'go' => 'here']);
|
||||
|
||||
.. versionadded:: 1.28
|
||||
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::
|
||||
If a template defines blocks, they can be rendered individually via the
|
||||
``renderBlock()`` call::
|
||||
|
||||
echo $template->renderBlock('block_name', array('the' => 'variables', 'go' => 'here'));
|
||||
echo $template->renderBlock('block_name', ['the' => 'variables', 'go' => 'here']);
|
||||
|
||||
.. _environment_options:
|
||||
|
||||
@@ -84,7 +84,7 @@ Environment Options
|
||||
When creating a new ``Twig_Environment`` instance, you can pass an array of
|
||||
options as the constructor second argument::
|
||||
|
||||
$twig = new Twig_Environment($loader, array('debug' => true));
|
||||
$twig = new Twig_Environment($loader, ['debug' => true]);
|
||||
|
||||
The following options are available:
|
||||
|
||||
@@ -183,7 +183,7 @@ load them::
|
||||
|
||||
It can also look for templates in an array of directories::
|
||||
|
||||
$loader = new Twig_Loader_Filesystem(array($templateDir1, $templateDir2));
|
||||
$loader = new Twig_Loader_Filesystem([$templateDir1, $templateDir2]);
|
||||
|
||||
With such a configuration, Twig will first look for templates in
|
||||
``$templateDir1`` and if they do not exist, it will fallback to look for them
|
||||
@@ -207,7 +207,7 @@ methods act on the "main" namespace)::
|
||||
Namespaced templates can be accessed via the special
|
||||
``@namespace_name/template_path`` notation::
|
||||
|
||||
$twig->render('@admin/index.html', array());
|
||||
$twig->render('@admin/index.html', []);
|
||||
|
||||
``Twig_Loader_Filesystem`` support absolute and relative paths. Using relative
|
||||
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)::
|
||||
``Twig_Loader_Array`` loads a template from a PHP array. It's passed an array
|
||||
of strings bound to template names::
|
||||
|
||||
$loader = new Twig_Loader_Array(array(
|
||||
$loader = new Twig_Loader_Array([
|
||||
'index.html' => 'Hello {{ name }}!',
|
||||
));
|
||||
]);
|
||||
$twig = new Twig_Environment($loader);
|
||||
|
||||
echo $twig->render('index.html', array('name' => 'Fabien'));
|
||||
echo $twig->render('index.html', ['name' => 'Fabien']);
|
||||
|
||||
This loader is very useful for unit testing. It can also be used for small
|
||||
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.
|
||||
|
||||
``Twig_Loader_Chain`` delegates the loading of templates to other loaders::
|
||||
|
||||
$loader1 = new Twig_Loader_Array(array(
|
||||
$loader1 = new Twig_Loader_Array([
|
||||
'base.html' => '{% block content %}{% endblock %}',
|
||||
));
|
||||
$loader2 = new Twig_Loader_Array(array(
|
||||
]);
|
||||
$loader2 = new Twig_Loader_Array([
|
||||
'index.html' => '{% extends "base.html" %}{% block content %}Hello {{ name }}{% endblock %}',
|
||||
'base.html' => 'Will never be loaded',
|
||||
));
|
||||
]);
|
||||
|
||||
$loader = new Twig_Loader_Chain(array($loader1, $loader2));
|
||||
$loader = new Twig_Loader_Chain([$loader1, $loader2]);
|
||||
|
||||
$twig = new Twig_Environment($loader);
|
||||
|
||||
@@ -476,15 +476,15 @@ by a policy instance. By default, Twig comes with one policy class:
|
||||
``Twig_Sandbox_SecurityPolicy``. This class allows you to white-list some
|
||||
tags, filters, properties, and methods::
|
||||
|
||||
$tags = array('if');
|
||||
$filters = array('upper');
|
||||
$methods = array(
|
||||
'Article' => array('getTitle', 'getBody'),
|
||||
);
|
||||
$properties = array(
|
||||
'Article' => array('title', 'body'),
|
||||
);
|
||||
$functions = array('range');
|
||||
$tags = ['if'];
|
||||
$filters = ['upper'];
|
||||
$methods = [
|
||||
'Article' => ['getTitle', 'getBody'],
|
||||
];
|
||||
$properties = [
|
||||
'Article' => ['title', 'body'],
|
||||
];
|
||||
$functions = ['range'];
|
||||
$policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
|
||||
|
||||
With the previous configuration, the security policy will only allow usage of
|
||||
|
||||
@@ -18,10 +18,10 @@ introspecting its variables:
|
||||
``Twig_Extension_Debug`` extension explicitly when creating your Twig
|
||||
environment::
|
||||
|
||||
$twig = new Twig_Environment($loader, array(
|
||||
$twig = new Twig_Environment($loader, [
|
||||
'debug' => true,
|
||||
// ...
|
||||
));
|
||||
]);
|
||||
$twig->addExtension(new Twig_Extension_Debug());
|
||||
|
||||
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
|
||||
// as of Twig 1.28
|
||||
$template = $twig->load('some_template.twig');
|
||||
|
||||
$twig->display('template.twig', array('template' => $template));
|
||||
$twig->display('template.twig', ['template' => $template]);
|
||||
|
||||
When you set the ``ignore_missing`` flag, Twig will return an empty string if
|
||||
the template does not exist:
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ using)::
|
||||
/* Hello {{ name }} */
|
||||
class __TwigTemplate_1121b6f109fe93ebe8c6e22e3712bceb extends Twig_Template
|
||||
{
|
||||
protected function doDisplay(array $context, array $blocks = array())
|
||||
protected function doDisplay(array $context, array $blocks = [])
|
||||
{
|
||||
// line 1
|
||||
echo "Hello ";
|
||||
|
||||
+6
-6
@@ -55,12 +55,12 @@ This section gives you a brief introduction to the PHP API for Twig.
|
||||
|
||||
require_once '/path/to/vendor/autoload.php';
|
||||
|
||||
$loader = new Twig_Loader_Array(array(
|
||||
$loader = new Twig_Loader_Array([
|
||||
'index' => 'Hello {{ name }}!',
|
||||
));
|
||||
]);
|
||||
$twig = new Twig_Environment($loader);
|
||||
|
||||
echo $twig->render('index', array('name' => 'Fabien'));
|
||||
echo $twig->render('index', ['name' => 'Fabien']);
|
||||
|
||||
Twig uses a loader (``Twig_Loader_Array``) to locate templates, and an
|
||||
environment (``Twig_Environment``) to store the configuration.
|
||||
@@ -72,11 +72,11 @@ As templates are generally stored on the filesystem, Twig also comes with a
|
||||
filesystem loader::
|
||||
|
||||
$loader = new Twig_Loader_Filesystem('/path/to/templates');
|
||||
$twig = new Twig_Environment($loader, array(
|
||||
$twig = new Twig_Environment($loader, [
|
||||
'cache' => '/path/to/compilation_cache',
|
||||
));
|
||||
]);
|
||||
|
||||
echo $twig->render('index.html', array('name' => 'Fabien'));
|
||||
echo $twig->render('index.html', ['name' => 'Fabien']);
|
||||
|
||||
.. tip::
|
||||
|
||||
|
||||
+42
-42
@@ -38,7 +38,7 @@ However, this code won't find all deprecations (like using deprecated some Twig
|
||||
classes). To catch all notices, register a custom error handler like the one
|
||||
below::
|
||||
|
||||
$deprecations = array();
|
||||
$deprecations = [];
|
||||
set_error_handler(function ($type, $msg) use (&$deprecations) {
|
||||
if (E_USER_DEPRECATED === $type) {
|
||||
$deprecations[] = $msg;
|
||||
@@ -163,37 +163,37 @@ To change the block delimiters, you need to create your own lexer object::
|
||||
|
||||
$twig = new Twig_Environment();
|
||||
|
||||
$lexer = new Twig_Lexer($twig, array(
|
||||
'tag_comment' => array('{#', '#}'),
|
||||
'tag_block' => array('{%', '%}'),
|
||||
'tag_variable' => array('{{', '}}'),
|
||||
'interpolation' => array('#{', '}'),
|
||||
));
|
||||
$lexer = new Twig_Lexer($twig, [
|
||||
'tag_comment' => ['{#', '#}'],
|
||||
'tag_block' => ['{%', '%}'],
|
||||
'tag_variable' => ['{{', '}}'],
|
||||
'interpolation' => ['#{', '}'],
|
||||
]);
|
||||
$twig->setLexer($lexer);
|
||||
|
||||
Here are some configuration example that simulates some other template engines
|
||||
syntax::
|
||||
|
||||
// Ruby erb syntax
|
||||
$lexer = new Twig_Lexer($twig, array(
|
||||
'tag_comment' => array('<%#', '%>'),
|
||||
'tag_block' => array('<%', '%>'),
|
||||
'tag_variable' => array('<%=', '%>'),
|
||||
));
|
||||
$lexer = new Twig_Lexer($twig, [
|
||||
'tag_comment' => ['<%#', '%>'],
|
||||
'tag_block' => ['<%', '%>'],
|
||||
'tag_variable' => ['<%=', '%>'],
|
||||
]);
|
||||
|
||||
// SGML Comment Syntax
|
||||
$lexer = new Twig_Lexer($twig, array(
|
||||
'tag_comment' => array('<!--#', '-->'),
|
||||
'tag_block' => array('<!--', '-->'),
|
||||
'tag_variable' => array('${', '}'),
|
||||
));
|
||||
$lexer = new Twig_Lexer($twig, [
|
||||
'tag_comment' => ['<!--#', '-->'],
|
||||
'tag_block' => ['<!--', '-->'],
|
||||
'tag_variable' => ['${', '}'],
|
||||
]);
|
||||
|
||||
// Smarty like
|
||||
$lexer = new Twig_Lexer($twig, array(
|
||||
'tag_comment' => array('{*', '*}'),
|
||||
'tag_block' => array('{', '}'),
|
||||
'tag_variable' => array('{$', '}'),
|
||||
));
|
||||
$lexer = new Twig_Lexer($twig, [
|
||||
'tag_comment' => ['{*', '*}'],
|
||||
'tag_block' => ['{', '}'],
|
||||
'tag_variable' => ['{$', '}'],
|
||||
]);
|
||||
|
||||
Using dynamic Object Properties
|
||||
-------------------------------
|
||||
@@ -233,12 +233,12 @@ Sometimes, when using nested loops, you need to access the parent context. The
|
||||
parent context is always accessible via the ``loop.parent`` variable. For
|
||||
instance, if you have the following template data::
|
||||
|
||||
$data = array(
|
||||
'topics' => array(
|
||||
'topic1' => array('Message 1 of topic 1', 'Message 2 of topic 1'),
|
||||
'topic2' => array('Message 1 of topic 2', 'Message 2 of topic 2'),
|
||||
),
|
||||
);
|
||||
$data = [
|
||||
'topics' => [
|
||||
'topic1' => ['Message 1 of topic 1', 'Message 2 of topic 1'],
|
||||
'topic2' => ['Message 1 of topic 2', 'Message 2 of topic 2'],
|
||||
],
|
||||
];
|
||||
|
||||
And the following template to display all messages in all topics:
|
||||
|
||||
@@ -345,10 +345,10 @@ cache won't update the cache.
|
||||
|
||||
To get around this, force Twig to invalidate the bytecode cache::
|
||||
|
||||
$twig = new Twig_Environment($loader, array(
|
||||
$twig = new Twig_Environment($loader, [
|
||||
'cache' => new Twig_Cache_Filesystem('/some/cache/path', Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION),
|
||||
// ...
|
||||
));
|
||||
]);
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -378,13 +378,13 @@ around, you probably want to reset it when visiting a new template.
|
||||
|
||||
This can be easily achieved with the following code::
|
||||
|
||||
protected $someTemplateState = array();
|
||||
protected $someTemplateState = [];
|
||||
|
||||
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_Module) {
|
||||
// reset the state as we are entering a new template
|
||||
$this->someTemplateState = array();
|
||||
$this->someTemplateState = [];
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -468,7 +468,7 @@ Now, let's define a loader able to use this database::
|
||||
protected function getValue($column, $name)
|
||||
{
|
||||
$sth = $this->dbh->prepare('SELECT '.$column.' FROM templates WHERE name = :name');
|
||||
$sth->execute(array(':name' => (string) $name));
|
||||
$sth->execute([':name' => (string) $name]);
|
||||
|
||||
return $sth->fetchColumn();
|
||||
}
|
||||
@@ -479,7 +479,7 @@ Finally, here is an example on how you can use it::
|
||||
$loader = new DatabaseTwigLoader($dbh);
|
||||
$twig = new Twig_Environment($loader);
|
||||
|
||||
echo $twig->render('index.twig', array('name' => 'Fabien'));
|
||||
echo $twig->render('index.twig', ['name' => 'Fabien']);
|
||||
|
||||
Using different Template Sources
|
||||
--------------------------------
|
||||
@@ -496,14 +496,14 @@ filesystem, or any other loader for that matter: the template name should be a
|
||||
logical name, and not the path from the filesystem::
|
||||
|
||||
$loader1 = new DatabaseTwigLoader($dbh);
|
||||
$loader2 = new Twig_Loader_Array(array(
|
||||
$loader2 = new Twig_Loader_Array([
|
||||
'base.twig' => '{% block content %}{% endblock %}',
|
||||
));
|
||||
$loader = new Twig_Loader_Chain(array($loader1, $loader2));
|
||||
]);
|
||||
$loader = new Twig_Loader_Chain([$loader1, $loader2]);
|
||||
|
||||
$twig = new Twig_Environment($loader);
|
||||
|
||||
echo $twig->render('index.twig', array('name' => 'Fabien'));
|
||||
echo $twig->render('index.twig', ['name' => 'Fabien']);
|
||||
|
||||
Now that the ``base.twig`` templates is defined in an array loader, you can
|
||||
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
|
||||
``Twig_Environment::createTemplate()`` (available as of Twig 1.18)::
|
||||
|
||||
$template = $twig->createTemplate('hello {{ name }}');
|
||||
echo $template->render(array('name' => 'Fabien'));
|
||||
echo $template->render(['name' => 'Fabien']);
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -561,8 +561,8 @@ include in your templates:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
$env->setLexer(new Twig_Lexer($env, array(
|
||||
'tag_variable' => array('{[', ']}'),
|
||||
)));
|
||||
$env->setLexer(new Twig_Lexer($env, [
|
||||
'tag_variable' => ['{[', ']}'],
|
||||
]));
|
||||
|
||||
.. _callback: https://secure.php.net/manual/en/function.is-callable.php
|
||||
|
||||
@@ -164,7 +164,7 @@ instance, Twig will use it as the parent template::
|
||||
// as of Twig 1.28
|
||||
$layout = $twig->load('some_layout_template.twig');
|
||||
|
||||
$twig->display('template.twig', array('layout' => $layout));
|
||||
$twig->display('template.twig', ['layout' => $layout]);
|
||||
|
||||
.. versionadded:: 1.2
|
||||
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
|
||||
// as of Twig 1.28
|
||||
$template = $twig->load('some_template.twig');
|
||||
|
||||
$twig->display('template.twig', array('template' => $template));
|
||||
$twig->display('template.twig', ['template' => $template]);
|
||||
|
||||
.. versionadded:: 1.2
|
||||
The ``ignore missing`` feature has been added in Twig 1.2.
|
||||
|
||||
Reference in New Issue
Block a user