mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-03 05:56:43 +00:00
added the possibility to give a nice name to string templates
This commit is contained in:
committed by
Fabien Potencier
parent
823299d212
commit
40fd7c4f86
+8
-2
@@ -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]),
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user