From: Fabien Potencier Date: Thu, 31 Oct 2013 12:34:32 +0000 (+0100) Subject: added a source function to include the content of a template without rendering it X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=dff7d2c7582a82515b995889b60a84c377d354c7;p=web%2Fkonrad%2Ftwig.git added a source function to include the content of a template without rendering it --- diff --git a/CHANGELOG b/CHANGELOG index 3181d05..6d4feaf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.15.0 (2013-XX-XX) + * added a source function to include the content of a template without rendering it * fixed the C extension sandbox behavior when get or set is prepend to method name * 1.14.2 (2013-10-30) diff --git a/doc/functions/index.rst b/doc/functions/index.rst index 8650cbd..5955e1f 100644 --- a/doc/functions/index.rst +++ b/doc/functions/index.rst @@ -14,4 +14,5 @@ Functions parent random range + source template_from_string diff --git a/doc/functions/source.rst b/doc/functions/source.rst new file mode 100644 index 0000000..c2c6adb --- /dev/null +++ b/doc/functions/source.rst @@ -0,0 +1,21 @@ +``source`` +========== + +.. versionadded:: 1.15 + The include function was added in Twig 1.15. + +The ``source`` function returns the content of a template without rendering it: + +.. code-block:: jinja + + {{ source('template.html') }} + {{ source(some_var) }} + +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. + +Arguments +--------- + +* ``name``: The name of the template to read diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 32d452d..d25e108 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -215,6 +215,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_SimpleFunction('random', 'twig_random', array('needs_environment' => true)), new Twig_SimpleFunction('date', 'twig_date_converter', array('needs_environment' => true)), new Twig_SimpleFunction('include', 'twig_include', array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))), + new Twig_SimpleFunction('source', 'twig_source', array('needs_environment' => true, 'is_safe' => array('all'))), ); } @@ -1371,6 +1372,18 @@ function twig_include(Twig_Environment $env, $context, $template, $variables = a } /** + * Returns a template content without rendering it. + * + * @param string $name The template name + * + * @return string The template source + */ +function twig_source(Twig_Environment $env, $name) +{ + return $env->getLoader()->getSource($name); +} + +/** * Provides the ability to get constants from instances as well as class/global constants. * * @param string $constant The name of the constant diff --git a/test/Twig/Tests/Fixtures/functions/source.test b/test/Twig/Tests/Fixtures/functions/source.test new file mode 100644 index 0000000..0e094c3 --- /dev/null +++ b/test/Twig/Tests/Fixtures/functions/source.test @@ -0,0 +1,17 @@ +--TEST-- +"source" function +--TEMPLATE-- +FOO +{{ source("foo.twig") }} + +BAR +--TEMPLATE(foo.twig)-- +{{ foo }}
+--DATA-- +return array() +--EXPECT-- +FOO + +{{ foo }}
+ +BAR