Completed
Push — master ( 37a8e4...ab7f39 )
by Jean-Christophe
02:01
created

Twig::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 13
nc 2
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
use Ubiquity\utils\base\UFileSystem;
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
}
54