From: Fabien Potencier Date: Sat, 11 Oct 2014 20:30:59 +0000 (+0200) Subject: made it clear that Twig_Loader_String must not be used by end users X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d12f7afbbeb64e2e8d4192f3a78cec913a0b45ae;p=web%2Fkonrad%2Ftwig.git made it clear that Twig_Loader_String must not be used by end users --- diff --git a/doc/api.rst b/doc/api.rst index b00d61e..456754b 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -166,21 +166,6 @@ Namespaced templates can be accessed via the special $twig->render('@admin/index.html', array()); -``Twig_Loader_String`` -...................... - -``Twig_Loader_String`` loads templates from strings. It's a dummy loader as -the template reference is the template source code:: - - $loader = new Twig_Loader_String(); - $twig = new Twig_Environment($loader); - - echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien')); - -This loader should only be used for unit testing as it has severe limitations: -several tags, like ``extends`` or ``include`` do not make sense to use as the -reference to the template is the template source code itself. - ``Twig_Loader_Array`` ..................... @@ -268,26 +253,6 @@ All loaders implement the ``Twig_LoaderInterface``:: function isFresh($name, $time); } -As an example, here is how the built-in ``Twig_Loader_String`` reads:: - - class Twig_Loader_String implements Twig_LoaderInterface - { - public function getSource($name) - { - return $name; - } - - public function getCacheKey($name) - { - return $name; - } - - public function isFresh($name, $time) - { - return false; - } - } - The ``isFresh()`` method must return ``true`` if the current cached template is still fresh, given the last modification time, or ``false`` otherwise. diff --git a/doc/intro.rst b/doc/intro.rst index 8677d50..c35e82a 100644 --- a/doc/intro.rst +++ b/doc/intro.rst @@ -50,12 +50,14 @@ This section gives you a brief introduction to the PHP API for Twig. require_once '/path/to/vendor/autoload.php'; - $loader = new Twig_Loader_String(); + $loader = new Twig_Loader_Array( + 'index' => 'Hello {{ name }}!', + ); $twig = new Twig_Environment($loader); - echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien')); + echo $twig->render('index', array('name' => 'Fabien')); -Twig uses a loader (``Twig_Loader_String``) to locate templates, and an +Twig uses a loader (``Twig_Loader_Array``) to locate templates, and an environment (``Twig_Environment``) to store the configuration. The ``render()`` method loads the template passed as a first argument and diff --git a/lib/Twig/Loader/Array.php b/lib/Twig/Loader/Array.php index ac56104..436edd8 100644 --- a/lib/Twig/Loader/Array.php +++ b/lib/Twig/Loader/Array.php @@ -17,6 +17,8 @@ * source code of the template). If you don't want to see your cache grows out of * control, you need to take care of clearing the old cache file by yourself. * + * This loader should only be used for unit testing. + * * @author Fabien Potencier */ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface diff --git a/lib/Twig/Loader/String.php b/lib/Twig/Loader/String.php index 8ad9856..2099c09 100644 --- a/lib/Twig/Loader/String.php +++ b/lib/Twig/Loader/String.php @@ -12,9 +12,7 @@ /** * Loads a template from a string. * - * This loader should only be used for unit testing as it has many limitations - * (for instance, the include or extends tag does not make any sense for a string - * loader). + * This loader should NEVER be used. It only exists for Twig internal purposes. * * When using this loader with a cache mechanism, you should know that a new cache * key is generated each time a template content "changes" (the cache key being the