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
|
|
|
use Ubiquity\translation\TranslatorManager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Ubiquity Twig template engine |
14
|
|
|
* @author jcheron <[email protected]> |
15
|
|
|
* @version 1.0.1 |
16
|
|
|
*/ |
17
|
|
|
class Twig extends TemplateEngine { |
18
|
|
|
private $twig; |
19
|
|
|
|
20
|
2 |
|
public function __construct($options=array()) { |
21
|
2 |
|
$loader=new \Twig_Loader_Filesystem(\ROOT . \DS . "views".\DS); |
22
|
2 |
|
$loader->addPath(implode(\DS,[Startup::getFrameworkDir(),"..","core","views"]).\DS,"framework"); |
23
|
2 |
|
if(isset($options["cache"]) && $options["cache"]===true) |
24
|
|
|
$options["cache"]=CacheManager::getCacheSubDirectory("views"); |
25
|
2 |
|
$this->twig=new \Twig_Environment($loader, $options); |
26
|
|
|
|
27
|
|
|
$function=new \Twig_SimpleFunction('path', function ($name,$params=[],$absolute=false) { |
28
|
1 |
|
return Router::path($name,$params,$absolute); |
29
|
2 |
|
}); |
30
|
2 |
|
$this->twig->addFunction($function); |
31
|
|
|
$function=new \Twig_SimpleFunction('url', function ($name,$params) { |
32
|
|
|
return Router::url($name,$params); |
33
|
2 |
|
}); |
34
|
2 |
|
$this->twig->addFunction($function); |
35
|
|
|
|
36
|
|
|
$function=new \Twig_SimpleFunction('t', function ($context,$id, array $parameters = array(), $domain = null, $locale = null) { |
37
|
|
|
$trans=TranslatorManager::trans($id,$parameters,$domain,$locale); |
38
|
|
|
return $this->twig->createTemplate($trans)->render($context); |
39
|
2 |
|
},['needs_context' => true]); |
40
|
2 |
|
$this->twig->addFunction($function); |
41
|
|
|
|
42
|
|
|
$test=new \Twig_SimpleTest('instanceOf', function($var,$class){ |
43
|
|
|
return $var instanceof $class; |
44
|
2 |
|
}); |
45
|
2 |
|
$this->twig->addTest($test); |
46
|
2 |
|
$this->twig->addGlobal("app", new Framework()); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* (non-PHPdoc) |
51
|
|
|
* @see TemplateEngine::render() |
52
|
|
|
*/ |
53
|
2 |
|
public function render($viewName, $pData, $asString) { |
54
|
2 |
|
$render=$this->twig->render($viewName, $pData); |
55
|
2 |
|
if ($asString) { |
56
|
|
|
return $render; |
57
|
|
|
} else{ |
58
|
2 |
|
echo $render; |
59
|
|
|
} |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
public function getBlockNames($templateName) { |
63
|
|
|
return $this->twig->load($templateName)->getBlockNames(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCode($templateName){ |
67
|
|
|
return UFileSystem::load($this->twig->load($templateName)->getSourceContext()->getPath()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|