-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the render function, deprecate the include one #4434
Open
fabpot
wants to merge
1
commit into
twigphp:3.x
Choose a base branch
from
fabpot:render-function
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing a Template instead of a TemplateWrapper is deprecated, afaik |
||
``\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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Let's deprecate the include tag too