Twig also comes with a filesystem loader:
[php]
- $loader = new Twig_Loader_Filesystem('/path/to/templates', '/path/to/compilation_cache');
- $twig = new Twig_Environment($loader);
+ $loader = new Twig_Loader_Filesystem('/path/to/templates');
+ $twig = new Twig_Environment($loader, array(
+ 'cache' => '/path/to/compilation_cache',
+ ));
$template = $twig->loadTemplate('index.html');
require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
- $loader = new Twig_Loader_Filesystem('/path/to/templates', '/path/to/compilation_cache');
- $twig = new Twig_Environment($loader);
+ $loader = new Twig_Loader_Filesystem('/path/to/templates');
+ $twig = new Twig_Environment($loader, array(
+ 'cache' => '/path/to/compilation_cache',
+ ));
This will create a template environment with the default settings and a loader
that looks up the templates in the `/path/to/templates/` folder. Different
loaders are available and you can also write your own if you want to load
templates from a database or other resources.
+>**CAUTION**
+>Before Twig 0.9.3, the `cache` option did not exist, and the cache directory
+>was passed as a second argument of the loader.
+
+-
+
>**NOTE**
->Notice that the second argument of the loader is a compilation cache
->directory, where Twig caches the compiled templates to avoid the parsing
->phase for sub-sequent requests. It is very different from the cache you might
->want to add for the evaluated templates. For such a need, you can use any
->available PHP cache library.
+>Notice that the second argument of the environment is an array of options.
+>The `cache` option is a compilation cache directory, where Twig caches the
+>compiled templates to avoid the parsing phase for sub-sequent requests. It
+>is very different from the cache you might want to add for the evaluated
+>templates. For such a need, you can use any available PHP cache library.
To load a template from this environment you just have to call the
`loadTemplate()` method which then returns a `Twig_Template` instance:
* `base_template_class`: The base template class to use for generated
templates (default to `Twig_Template`).
+ * `cache`: It can take three values:
+
+ * `null` (the default): Twig will create a sub-directory under the system
+ temp directory to store the compiled templates (not recommended as
+ templates from two projects with the same name will share the same cache if
+ your projects share the same Twig source code).
+
+ * `false`: disable the compile cache altogether (not recommended).
+
+ * An absolute path where to store the compiled templates.
+
+ * `auto_reload`: When developing with Twig, it's useful to recompile the
+ template whenever the source code changes. This is the default behavior for
+ `Twig_Loader_Filesystem` for instance. In a production environment, you can
+ set this option to `false` for better performance. If you don't provide the
+ `auto_reload` option, it will be determined automatically base on the
+ `debug` value.
+
+>**CAUTION**
+>Before Twig 0.9.3, the `cache` and `auto_reload` options did not exist. They
+>was passed as a second and third arguments of the filesystem loader
+>respectively.
+
Loaders
-------
All template loaders can cache the compiled templates on the filesystem for
future reuse. It speeds up Twig a lot as the templates are only compiled once;
and the performance boost is even larger if you use a PHP accelerator such as
-APC.
-
-The compilation cache can take three values:
-
- * `null` (the default): Twig will create a sub-directory under the system
- temp directory to store the compiled templates (not recommended as
- templates from two projects with the same name will share the same cache if
- your projects share the same Twig source code).
-
- * `false`: disable the compile cache altogether (not recommended).
-
- * An absolute path where to store the compiled templates.
-
-### Auto-reload
-
-When developing with Twig, it's useful to recompile the template whenever the
-source code changes. This is the default behavior for `Twig_Loader_Filesystem`
-for instance. In a production environment, you can turn this option off for
-better performance:
-
- [php]
- $loader = new Twig_Loader_Filesystem($templateDir, $cacheDir, false);
+APC. See the `cache` and `auto_reload` options of `Twig_Environment` above for
+more information.
### Built-in Loaders
to load them.
[php]
- $loader = new Twig_Loader_Filesystem($templateDir, $cacheDir, $autoReload);
+ $loader = new Twig_Loader_Filesystem($templateDir);
* `Twig_Loader_String`: Loads templates from a string. It's a dummy loader as
you pass it the source code directly.
[php]
- $loader = new Twig_Loader_String($cacheDir, $autoReload);
+ $loader = new Twig_Loader_String($cacheDir);
* `Twig_Loader_Array`: Loads a template from a PHP array. It's passed an
array of strings bound to template names. This loader is useful for unit
testing.
[php]
- $loader = new Twig_Loader_Array($templates, $cacheDir);
+ $loader = new Twig_Loader_Array($templates);
### Create your own Loader
* @return string The class name of the compiled template
*/
public function load($name);
+
+ /**
+ * Sets the Environment related to this loader.
+ *
+ * @param Twig_Environment $env A Twig_Environment instance
+ */
+ public function setEnvironment(Twig_Environment $env);
}
But if you want to create your own loader, you'd better inherit from the
protected $loader;
protected $trimBlocks;
protected $debug;
+ protected $autoReload;
+ protected $cache;
protected $lexer;
protected $parser;
protected $compiler;
protected $runtimeInitialized;
protected $loadedTemplates;
+ /**
+ * Constructor.
+ *
+ * Available options:
+ *
+ * * debug: When set to `true`, the generated templates have a __toString()
+ * method that you can use to display the generated nodes (default to
+ * false).
+ *
+ * * trim_blocks: Mimicks the behavior of PHP by removing the newline that
+ * follows instructions if present (default to false).
+ *
+ * * charset: The charset used by the templates (default to utf-8).
+ *
+ * * base_template_class: The base template class to use for generated
+ * templates (default to Twig_Template).
+ *
+ * * cache: Can be one of three values:
+ * * null (the default): Twig will create a sub-directory under the system tmp directory
+ * (not recommended as templates from two projects with the same name will share the cache)
+ * * false: disable the compile cache altogether
+ * * An absolute path where to store the compiled templates
+ *
+ * * auto_reload: Whether to reload the template is the original source changed.
+ * If you don't provide the auto_reload option, it will be
+ * determined automatically base on the debug value.
+ */
public function __construct(Twig_LoaderInterface $loader = null, $options = array())
{
if (null !== $loader)
$this->trimBlocks = isset($options['trim_blocks']) ? (bool) $options['trim_blocks'] : false;
$this->charset = isset($options['charset']) ? $options['charset'] : 'UTF-8';
$this->baseTemplateClass = isset($options['base_template_class']) ? $options['base_template_class'] : 'Twig_Template';
+ $this->autoReload = isset($options['auto_reload']) ? (bool) $options['auto_reload'] : $this->debug;
$this->extensions = array(new Twig_Extension_Core());
$this->runtimeInitialized = false;
+ $this->setCache(isset($options['cache']) ? $options['cache'] : null);
}
public function getBaseTemplateClass()
return $this->debug;
}
+ public function isAutoReload()
+ {
+ return $this->autoReload;
+ }
+
+ public function setAutoReload($autoReload)
+ {
+ $this->autoReload = (Boolean) $autoReload;
+ }
+
+ public function getCache()
+ {
+ return $this->cache;
+ }
+
+ public function setCache($cache)
+ {
+ $this->cache = null === $cache ? sys_get_temp_dir().DIRECTORY_SEPARATOR.'twig_'.md5(dirname(__FILE__)) : $cache;
+
+ if (false !== $this->cache && !is_dir($this->cache))
+ {
+ mkdir($this->cache, 0755, true);
+ }
+ }
+
+ public function getCacheFilename($name)
+ {
+ return $this->getCache() ? $this->getCache().'/twig_'.md5($name).'.php' : false;
+ }
+
public function getTrimBlocks()
{
return $this->trimBlocks;
*/
abstract class Twig_Loader implements Twig_LoaderInterface
{
- protected $cache;
- protected $autoReload;
protected $env;
/**
- * Constructor.
- *
- * The cache can be one of three values:
- *
- * * null (the default): Twig will create a sub-directory under the system tmp directory
- * (not recommended as templates from two projects with the same name will share the cache)
- *
- * * false: disable the compile cache altogether
- *
- * * An absolute path where to store the compiled templates
- *
- * @param string $cache The compiler cache directory
- * @param Boolean $autoReload Whether to reload the template is the original source changed
- */
- public function __construct($cache = null, $autoReload = true)
- {
- $this->cache = null === $cache ? sys_get_temp_dir().DIRECTORY_SEPARATOR.'twig_'.md5(dirname(__FILE__)) : $cache;
-
- if (false !== $this->cache && !is_dir($this->cache))
- {
- mkdir($this->cache, 0755, true);
- }
-
- $this->autoReload = $autoReload;
- }
-
- /**
* Loads a template by name.
*
* @param string $name The template name
return $cls;
}
- if (false === $this->cache)
+ if (false === $cache = $this->env->getCacheFilename($name))
{
list($source, ) = $this->getSource($name);
$this->evalString($source, $name);
return $cls;
}
- $cache = $this->getCacheFilename($name);
if (!file_exists($cache))
{
list($source, $mtime) = $this->getSource($name);
$this->save($this->compile($source, $name), $cache);
}
- elseif ($this->autoReload)
+ elseif ($this->env->isAutoReload())
{
list($source, $mtime) = $this->getSource($name);
if (filemtime($cache) < $mtime)
$this->env = $env;
}
- public function getCacheFilename($name)
- {
- return $this->cache.'/twig_'.md5($name).'.php';
- }
-
protected function compile($source, $name)
{
return $this->env->compile($this->env->parse($this->env->tokenize($source, $name)));
/**
* Constructor.
*
- * @param array $templates An array of templates (keys are the names, and values are the source code)
- * @param string $cache The compiler cache directory
- * @param Boolean $autoReload Whether to reload the template is the original source changed
+ * @param array $templates An array of templates (keys are the names, and values are the source code)
*
* @see Twig_Loader
*/
- public function __construct(array $templates, $cache = null)
+ public function __construct(array $templates)
{
- parent::__construct($cache);
-
$this->templates = array();
foreach ($templates as $name => $template)
{
/**
* Constructor.
*
- * @param string|array $paths A path or an array of paths where to look for templates
- * @param string $cache The compiler cache directory
- * @param Boolean $autoReload Whether to reload the template is the original source changed
- *
- * @see Twig_Loader
+ * @param string|array $paths A path or an array of paths where to look for templates
*/
- public function __construct($paths, $cache = null, $autoReload = true)
+ public function __construct($paths)
{
$this->setPaths($paths);
-
- parent::__construct($cache, $autoReload);
}
/**