1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\ApplicationLatte;
9:
10: use Nette;
11: use Nette\Application\UI;
12:
13:
14: 15: 16:
17: class TemplateFactory extends Nette\Object implements UI\ITemplateFactory
18: {
19:
20: private $latteFactory;
21:
22:
23: private $httpRequest;
24:
25:
26: private $httpResponse;
27:
28:
29: private $user;
30:
31:
32: private $cacheStorage;
33:
34:
35: public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $httpRequest = NULL,
36: Nette\Http\IResponse $httpResponse = NULL, Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL)
37: {
38: $this->latteFactory = $latteFactory;
39: $this->httpRequest = $httpRequest;
40: $this->httpResponse = $httpResponse;
41: $this->user = $user;
42: $this->cacheStorage = $cacheStorage;
43: }
44:
45:
46: 47: 48:
49: public function createTemplate(UI\Control $control = NULL)
50: {
51: $latte = $this->latteFactory->create();
52: $template = new Template($latte);
53: $presenter = $control ? $control->getPresenter(FALSE) : NULL;
54:
55: if ($control instanceof UI\Presenter) {
56: $latte->setLoader(new Loader($control));
57: }
58:
59: if ($latte->onCompile instanceof \Traversable) {
60: $latte->onCompile = iterator_to_array($latte->onCompile);
61: }
62:
63: array_unshift($latte->onCompile, function ($latte) use ($control, $template) {
64: $latte->getParser()->shortNoEscape = TRUE;
65: $latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler()));
66: UIMacros::install($latte->getCompiler());
67: if (class_exists('Nette\Bridges\FormsLatte\FormMacros')) {
68: Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
69: }
70: if ($control) {
71: $control->templatePrepareFilters($template);
72: }
73: });
74:
75: $latte->addFilter('url', 'rawurlencode');
76: foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) {
77: $latte->addFilter($name, 'Nette\Utils\Strings::' . $name);
78: }
79: $latte->addFilter('null', function () {});
80: $latte->addFilter('length', function ($var) {
81: return is_string($var) ? Nette\Utils\Strings::length($var) : count($var);
82: });
83: $latte->addFilter('modifyDate', function ($time, $delta, $unit = NULL) {
84: return $time == NULL ? NULL : Nette\Utils\DateTime::from($time)->modify($delta . $unit);
85: });
86:
87: if (!array_key_exists('translate', $latte->getFilters())) {
88: $latte->addFilter('translate', function () {
89: throw new Nette\InvalidStateException('Translator has not been set. Set translator using $template->setTranslator().');
90: });
91: }
92:
93:
94: $template->control = $template->_control = $control;
95: $template->presenter = $template->_presenter = $presenter;
96: $template->user = $this->user;
97: $template->netteHttpResponse = $this->httpResponse;
98: $template->netteCacheStorage = $this->cacheStorage;
99: $template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL;
100: $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl);
101: $template->flashes = array();
102:
103: if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) {
104: $id = $control->getParameterId('flash');
105: $template->flashes = (array) $presenter->getFlashSession()->$id;
106: }
107:
108: return $template;
109: }
110:
111: }
112: