Completed
Push — master ( 0db593...5f0535 )
by Jean-Christophe
01:58
created

Twig::getBlockNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ubiquity\views\engine;
4
5
use Ubiquity\controllers\Startup;
6
use Ubiquity\controllers\Router;
7
use Ubiquity\cache\CacheManager;
8
use Ubiquity\core\Framework;
9
10
class Twig extends TemplateEngine {
11
	private $twig;
12
13
	public function __construct($options=array()) {
14
		$loader=new \Twig_Loader_Filesystem(ROOT . DS . "views/");
15
		$loader->addPath(implode(DS,[Startup::getFrameworkDir(),"..","core","views"]).DS,"framework");
16
		if(isset($options["cache"]) && $options["cache"]===true)
17
			$options["cache"]=ROOT.DS.CacheManager::getCacheDirectory().DS."views/";
18
		$this->twig=new \Twig_Environment($loader, $options);
19
20
		$function=new \Twig_SimpleFunction('path', function ($name,$params=[],$absolute=false) {
21
			return Router::path($name,$params,$absolute);
22
		});
23
		$this->twig->addFunction($function);
24
		$function=new \Twig_SimpleFunction('url', function ($name,$params) {
25
			return Router::url($name,$params);
26
		});
27
		$this->twig->addFunction($function);
28
		$this->twig->addGlobal("app", new Framework());
29
	}
30
31
	/*
32
	 * (non-PHPdoc)
33
	 * @see TemplateEngine::render()
34
	 */
35
	public function render($viewName, $pData, $asString) {
36
		$pData["config"]=Startup::getConfig();
37
		$render=$this->twig->render($viewName, $pData);
38
		if ($asString) {
39
			return $render;
40
		} else
41
			echo $render;
42
	}
43
44
	public function getBlockNames($templateName) {
45
		return $this->twig->load($templateName)->getBlockNames();
46
	}
47
48
}
49