-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the render function, deprecate the include one
- Loading branch information
Showing
51 changed files
with
462 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Functions | |
html_classes | ||
html_cva | ||
include | ||
render | ||
max | ||
min | ||
parent | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
``render`` | ||
========== | ||
|
||
.. versionadded:: 3.15 | ||
|
||
The ``render`` function was added in Twig 3.15. | ||
|
||
The ``render`` function returns the rendered content of a template: | ||
|
||
.. code-block:: twig | ||
{{ render('template.html') }} | ||
Templates | ||
--------- | ||
|
||
Templates are loaded via the current Twig loader. | ||
|
||
Templates can be a string or an expression evaluating to a string: | ||
|
||
.. code-block:: twig | ||
{{ render('template.html') }} | ||
{{ render(template_var) }} | ||
A template can also be an instance of ``\Twig\Template`` or a | ||
``\Twig\TemplateWrapper``:: | ||
|
||
$template = $twig->load('some_template.twig'); | ||
$twig->display('template.twig', ['template' => $template]); | ||
|
||
// You can render it like this: | ||
// {{ render(template) }} | ||
|
||
When you set the ``ignore_missing`` flag, Twig will return an empty string if | ||
the template does not exist: | ||
|
||
.. code-block:: twig | ||
{{ render('sidebar.html', ignore_missing = true) }} | ||
You can also provide a list of templates that are checked for existence. The | ||
first template that exists will be rendered: | ||
|
||
.. code-block:: twig | ||
{{ render(['page_detailed.html', 'page.html']) }} | ||
If ``ignore_missing`` is set, it will fall back to rendering nothing if none | ||
of the templates exist, otherwise it will throw an exception. | ||
|
||
Context | ||
------- | ||
|
||
Rendered templates **do not** have access to the variables defined in the | ||
context, but you can pass some explicitly: | ||
|
||
.. code-block:: twig | ||
{# template.html will have access to the "name" variable #} | ||
{{ render('template.html', { name: 'Fabien' }) }} | ||
When passing a variable from the current context, you can use the following | ||
shortcut: | ||
|
||
.. code-block:: twig | ||
{{ render('template.html', { first_name, last_name }) }} | ||
{# is equivalent to #} | ||
{{ render('template.html', { first_name: first_name, last_name: last_name }) }} | ||
Sandboxing | ||
---------- | ||
|
||
When rendering a template created by an end user, you should consider | ||
:doc:`sandboxing<../sandbox>` it: | ||
|
||
.. code-block:: twig | ||
{{ render('page.html', sandboxed: true) }} | ||
Arguments | ||
--------- | ||
|
||
* ``template``: The template to render | ||
* ``variables``: The variables to pass to the template | ||
* ``ignore_missing``: Whether to ignore missing templates or not | ||
* ``sandboxed``: Whether to sandbox the template or not |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
tests/Fixtures/exceptions/multiline_function_with_undefined_variable.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
--TEST-- | ||
Exception for multile function with undefined variable | ||
Exception for multi-line function with undefined variable | ||
--TEMPLATE-- | ||
{{ include('foo', | ||
with_context=with_context | ||
{{ render('foo', | ||
sandboxed=with_sandbox | ||
) }} | ||
--TEMPLATE(foo)-- | ||
Foo | ||
--DATA-- | ||
return [] | ||
--EXCEPTION-- | ||
Twig\Error\RuntimeError: Variable "with_context" does not exist in "index.twig" at line 3. | ||
Twig\Error\RuntimeError: Variable "with_sandbox" does not exist in "index.twig" at line 3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
"include" function | ||
--DEPRECATION-- | ||
Since twig/twig 3.15: Twig Function "include" is deprecated; use "render" instead in index.twig at line 2. | ||
--TEMPLATE-- | ||
{% set tmp = include("foo.twig") %} | ||
|
||
FOO{{ tmp }}BAR | ||
--TEMPLATE(foo.twig)-- | ||
FOOBAR | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
FOO | ||
FOOBARBAR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
"include" function is safe for auto-escaping | ||
--DEPRECATION-- | ||
Since twig/twig 3.15: Twig Function "include" is deprecated; use "render" instead in index.twig at line 2. | ||
--TEMPLATE-- | ||
{{ include("foo.twig") }} | ||
--TEMPLATE(foo.twig)-- | ||
<p>Test</p> | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
<p>Test</p> |
Oops, something went wrong.