mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-17 21:07:18 +00:00
add ignore_missing agrument to source function
This commit is contained in:
committed by
Fabien Potencier
parent
793ea7e37d
commit
afa93586bc
@@ -11,6 +11,13 @@ The ``source`` function returns the content of a template without rendering it:
|
|||||||
{{ source('template.html') }}
|
{{ source('template.html') }}
|
||||||
{{ source(some_var) }}
|
{{ 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
|
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
|
templates. So, if you are using the filesystem loader, the templates are looked
|
||||||
for in the paths defined by it.
|
for in the paths defined by it.
|
||||||
@@ -19,3 +26,4 @@ Arguments
|
|||||||
---------
|
---------
|
||||||
|
|
||||||
* ``name``: The name of the template to read
|
* ``name``: The name of the template to read
|
||||||
|
* ``ignore_missing``: Whether to ignore missing templates or not
|
||||||
|
|||||||
@@ -1450,13 +1450,20 @@ function twig_include(Twig_Environment $env, $context, $template, $variables = a
|
|||||||
/**
|
/**
|
||||||
* Returns a template content without rendering it.
|
* 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
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user