added the possibility to give a nice name to string templates

This commit is contained in:
Thomas Landauer
2019-04-08 17:23:16 +02:00
committed by Fabien Potencier
parent 823299d212
commit 40fd7c4f86
2 changed files with 11 additions and 4 deletions
+8 -2
View File
@@ -515,15 +515,21 @@ 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
*
* @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)
public function createTemplate($template, string $name=null)
{
$name = sprintf('__string_template__%s', hash('sha256', $template, false));
if ($name) {
$name = "string_template '$name'";
}
else {
$name = sprintf('__string_template__%s', hash('sha256', $template, false));
}
$loader = new ChainLoader([
new ArrayLoader([$name => $template]),
+3 -2
View File
@@ -43,11 +43,12 @@ use Twig\Template;
* {{ include(template_from_string("Hello {{ name }}")) }}
*
* @param string $template A template as a string or object implementing __toString()
* @param string $name An optional name of the template to be used in error messages
*
* @return Template
*/
function twig_template_from_string(Environment $env, $template)
function twig_template_from_string(Environment $env, $template, string $name=null)
{
return $env->createTemplate((string) $template);
return $env->createTemplate((string) $template, $name);
}
}