mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 03:46:39 +00:00
made it clear that Twig_Loader_String must not be used by end users
This commit is contained in:
-35
@@ -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
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user