add ignore_missing agrument to source function

This commit is contained in:
Valeriy Trubachev
2015-03-31 11:35:00 -05:00
committed by Fabien Potencier
parent 793ea7e37d
commit afa93586bc
2 changed files with 18 additions and 3 deletions
+8
View File
@@ -11,6 +11,13 @@ The ``source`` function returns the content of a template without rendering it:
{{ source('template.html') }}
{{ source(some_var) }}
When you set the ``ignore_missing`` flag, Twig will return an empty string if
the template does not exist:
.. code-block:: jinja
{{ source('template.html', ignore_missing = true) }}
The function uses the same template loaders as the ones used to include
templates. So, if you are using the filesystem loader, the templates are looked
for in the paths defined by it.
@@ -19,3 +26,4 @@ Arguments
---------
* ``name``: The name of the template to read
* ``ignore_missing``: Whether to ignore missing templates or not
+10 -3
View File
@@ -1450,13 +1450,20 @@ function twig_include(Twig_Environment $env, $context, $template, $variables = a
/**
* Returns a template content without rendering it.
*
* @param string $name The template name
* @param string $name The template name
* @param bool $ignore_missing Whether to ignore missing templates or not
*
* @return string The template source
*/
function twig_source(Twig_Environment $env, $name)
function twig_source(Twig_Environment $env, $name, $ignoreMissing = false)
{
return $env->getLoader()->getSource($name);
try {
return $env->getLoader()->getSource($name);
} catch (Twig_Error_Loader $e) {
if (!$ignoreMissing) {
throw $e;
}
}
}
/**