4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Loads a template from an array.
15 * When using this loader with a cache mechanism, you should know that a new cache
16 * key is generated each time a template content "changes" (the cache key being the
17 * source code of the template). If you don't want to see your cache grows out of
18 * control, you need to take care of clearing the old cache file by yourself.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class Twig_Loader_Array
implements Twig_LoaderInterface
, Twig_ExistsLoaderInterface
29 * @param array $templates An array of templates (keys are the names, and values are the source code)
33 public function __construct(array $templates)
35 $this->templates
= array();
36 foreach ($templates as $name => $template) {
37 $this->templates
[$name] = $template;
42 * Adds or overrides a template.
44 * @param string $name The template name
45 * @param string $template The template source
47 public function setTemplate($name, $template)
49 $this->templates
[(string) $name] = $template;
55 public function getSource($name)
57 $name = (string) $name;
58 if (!isset($this->templates
[$name])) {
59 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
62 return $this->templates
[$name];
68 public function exists($name)
70 return isset($this->templates
[(string) $name]);
76 public function getCacheKey($name)
78 $name = (string) $name;
79 if (!isset($this->templates
[$name])) {
80 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
83 return $this->templates
[$name];
89 public function isFresh($name, $time)
91 $name = (string) $name;
92 if (!isset($this->templates
[$name])) {
93 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));