Ubiquity  2.0.3
php rapid development framework
Twig.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\views\engine;
4 
10 
11 class Twig extends TemplateEngine {
12  private $twig;
13 
14  public function __construct($options=array()) {
15  $loader=new \Twig_Loader_Filesystem(ROOT . DS . "views/");
16  $loader->addPath(implode(DS,[Startup::getFrameworkDir(),"..","core","views"]).DS,"framework");
17  if(isset($options["cache"]) && $options["cache"]===true)
18  $options["cache"]=ROOT.DS.CacheManager::getCacheDirectory().DS."views/";
19  $this->twig=new \Twig_Environment($loader, $options);
20 
21  $function=new \Twig_SimpleFunction('path', function ($name,$params=[],$absolute=false) {
22  return Router::path($name,$params,$absolute);
23  });
24  $this->twig->addFunction($function);
25  $function=new \Twig_SimpleFunction('url', function ($name,$params) {
26  return Router::url($name,$params);
27  });
28  $this->twig->addFunction($function);
29  $this->twig->addGlobal("app", new Framework());
30  }
31 
32  /*
33  * (non-PHPdoc)
34  * @see TemplateEngine::render()
35  */
36  public function render($viewName, $pData, $asString) {
37  $pData["config"]=Startup::getConfig();
38  $render=$this->twig->render($viewName, $pData);
39  if ($asString) {
40  return $render;
41  } else
42  echo $render;
43  }
44 
45  public function getBlockNames($templateName) {
46  return $this->twig->load($templateName)->getBlockNames();
47  }
48 
49  public function getCode($templateName){
50  return UFileSystem::load($this->twig->load($templateName)->getSourceContext()->getPath());
51  }
52 
53 }
static url($name, $parameters=[])
Returns the generated url from a route.
Definition: Router.php:146
__construct($options=array())
Definition: Twig.php:14
render($viewName, $pData, $asString)
Definition: Twig.php:36
getBlockNames($templateName)
Definition: Twig.php:45
static path($name, $parameters=[], $absolute=false)
Returns the generated path from a route.
Definition: Router.php:133
getCode($templateName)
Definition: Twig.php:49