made it clear that Twig_Loader_String must not be used by end users

This commit is contained in:
Fabien Potencier
2014-10-11 22:30:59 +02:00
parent 0de848bd6e
commit d12f7afbbe
4 changed files with 8 additions and 41 deletions
-35
View File
@@ -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.
+5 -3
View File
@@ -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
+2
View File
@@ -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 <fabien@symfony.com>
*/
class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
+1 -3
View File
@@ -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