Skip to content

Instantly share code, notes, and snippets.

@yosymfony yosymfony/TwigFactory.php
Last active Dec 22, 2015

Embed
What would you like to do?
A Twig factory
<?php
require_once '/path/to/lib/Twig/Autoloader.php';
/**
* A factory for create Twig instances
*
* @author Victor Puertas <vpgugr@gmail.com>
*
* Usage:
* <code>
* $twig = new TwigFactory();
* $twig->withAutoescape(false)
* ->withCache(false) // or withCache('your/path')
* ->addLoadrFilesystem(array(
* 'your/template/path',
* 'namespace' => 'your/template/path2',
* ))
* ->addLoaderString()
* ->create();
* </code>
*/
class TwigFactory
{
private $loader;
private $enviromentOpt = array();
public function __construct()
{
\Twig_Autoloader::register();
$loader = new \Twig_Loader_Chain();
}
/**
* Set cache path
*
* @param $value mixed Path to cache dir or false to disable cache
*
* @return TwigFactory Fluent interface
*/
public function withCache($value)
{
$this->enviromentOpt['cache'] = $value;
return $this;
}
/**
* Activate debug mode
*
* @param bool $value
*
* @return TwigFactory Fluent interface
*/
public function withDebug($value)
{
$this->enviromentOpt['debug'] = $value;
return $this;
}
/**
* Set the autoescape strategy
*
* @param $value mixed css, url, html_attr, callback function or false to disable
*
* @return TwigFactory Fluent interface
*/
public function withAutoescape($value)
{
$this->enviromentOpt['autoescape'] = $value;
return $this;
}
/**
* Add String loader
*
* @return TwigFactory Fluent interface
*/
public function addLoaderString()
{
$this->loader[] = new \Twig_Loader_String();
return $this;
}
/**
* Add Array loader
*
* @param array $values
*
* @return TwigFactory Fluent interface
*/
public function addLoaderArray(array $values)
{
$this->loader[] = new \Twig_Loader_Array($values);
return $this;
}
/**
* Add Filesystem loader.
* Usage:
* <code>
* // Simple paths
* $factory->addLoaderFilesystem('your/path/to/templates');
*
* // Multiple paths
* $factory->addLoaderFilesystem(array('your/path1/to/templates', 'your/path2/to/templates'));
*
* // With namespaces
* $factory->addLoaderFilesystem(array(
* 'namespace1' => 'your/path1/to/templates',
* 'namespace2' => 'your/path2/to/templates',
* ));
* </code>
*
* @return TwigFactory Fluent interface
*/
public function addLoaderFilesystem($paths)
{
$loader = new \Twig_Loader_Filesystem();
if(is_string($paths))
{
$loader->addPath($paths);
}
if(is_array($paths))
{
foreach($paths as $namespace => $path)
{
if(is_string($namespace))
{
$loader->addPath($path, $namespace);
}
else
{
$loader->addPath($path);
}
}
}
$this->loader[] = $loader;
return $this;
}
/**
* Create a Twig instance
*
* @return Twig_Environment
*/
public function create()
{
$twigLoader = new \Twig_Loader_Chain($this->loader);
$twig = new \Twig_Environment($twigLoader, $this->enviromentOpt);
return $twig;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.