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