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