added the possibility to give a nice name to string templates

This commit is contained in:
Fabien Potencier
2019-04-11 09:53:41 +02:00
parent 40fd7c4f86
commit d7e2e5418c
5 changed files with 28 additions and 8 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.39.0 (2019-XX-XX)
* added the possibility to give a nice name to string templates (template_from_string function)
* fixed the "with" behavior to always include the globals (for consistency with the "include" and "embed" tags)
* fixed "include" with "ignore missing" when an error loading occurs in the included template
* added support for a new whitespace trimming option ({%~ ~%}, {{~ ~}}, {#~ ~#})
+11
View File
@@ -4,6 +4,9 @@
.. versionadded:: 1.11
The ``template_from_string`` function was added in Twig 1.11.
.. versionadded:: 1.39
The name argument was added in Twig 1.39.
The ``template_from_string`` function loads a template from a string:
.. code-block:: jinja
@@ -11,6 +14,13 @@ The ``template_from_string`` function loads a template from a string:
{{ include(template_from_string("Hello {{ name }}")) }}
{{ include(template_from_string(page.template)) }}
To ease debugging, you can also give the template a name that will be part of
any related error message:
.. code-block:: jinja
{{ include(template_from_string(page.template, "template for page " ~ page.name)) }}
.. note::
The ``template_from_string`` function is not available by default. You
@@ -30,3 +40,4 @@ Arguments
---------
* ``template``: The template
* ``name``: A name for the template
+7 -7
View File
@@ -515,20 +515,20 @@ class Environment
* This method should not be used as a generic way to load templates.
*
* @param string $template The template name
* @param string $name An optional name of the template to be used in error messages
* @param string $name An optional name of the template to be used in error messages
*
* @return TemplateWrapper A template instance representing the given template name
*
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
*/
public function createTemplate($template, string $name=null)
public function createTemplate($template, string $name = null)
{
if ($name) {
$name = "string_template '$name'";
}
else {
$name = sprintf('__string_template__%s', hash('sha256', $template, false));
$hash = hash('sha256', $template, false);
if (null !== $name) {
$name = sprintf('%s (string template %s)', $name, $hash);
} else {
$name = sprintf('__string_template__%s', $hash);
}
$loader = new ChainLoader([
+1 -1
View File
@@ -47,7 +47,7 @@ use Twig\Template;
*
* @return Template
*/
function twig_template_from_string(Environment $env, $template, string $name=null)
function twig_template_from_string(Environment $env, $template, string $name = null)
{
return $env->createTemplate((string) $template, $name);
}
@@ -0,0 +1,8 @@
--TEST--
"template_from_string" function
--TEMPLATE--
{% include template_from_string("{{ not a Twig template ", "foo.twig") %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Unclosed "variable" in "foo.twig (string template 4900163d56b1af4b704c6b0afee7f98ba53418ce7a93d37a3af1882735baf9cd)" at line 1.