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: 18:
19: class TemplateFactory extends Nette\Object implements UI\ITemplateFactory
20: {
21:
22: private $latteFactory;
23:
24:
25: private $httpRequest;
26:
27:
28: private $httpResponse;
29:
30:
31: private $user;
32:
33:
34: private $cacheStorage;
35:
36:
37: public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $httpRequest = NULL,
38: Nette\Http\IResponse $httpResponse = NULL, Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL)
39: {
40: $this->latteFactory = $latteFactory;
41: $this->httpRequest = $httpRequest;
42: $this->httpResponse = $httpResponse;
43: $this->user = $user;
44: $this->cacheStorage = $cacheStorage;
45: }
46:
47:
48: 49: 50:
51: public function createTemplate(UI\Control $control)
52: {
53: $latte = $this->latteFactory->create();
54: $template = new Template($latte);
55: $presenter = $control->getPresenter(FALSE);
56:
57: if ($control instanceof UI\Presenter) {
58: $latte->setLoader(new Loader($control));
59: }
60:
61: if ($latte->onCompile instanceof \Traversable) {
62: $latte->onCompile = iterator_to_array($latte->onCompile);
63: }
64:
65: array_unshift($latte->onCompile, function ($latte) use ($control, $template) {
66: $latte->getParser()->shortNoEscape = TRUE;
67: $latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler()));
68: UIMacros::install($latte->getCompiler());
69: if (class_exists('Nette\Bridges\FormsLatte\FormMacros')) {
70: Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
71: }
72: $control->templatePrepareFilters($template);
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:
88: $template->control = $template->_control = $control;
89: $template->presenter = $template->_presenter = $presenter;
90: $template->user = $this->user;
91: $template->netteHttpResponse = $this->httpResponse;
92: $template->netteCacheStorage = $this->cacheStorage;
93: $template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL;
94: $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl);
95: $template->flashes = array();
96:
97: if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) {
98: $id = $control->getParameterId('flash');
99: $template->flashes = (array) $presenter->getFlashSession()->$id;
100: }
101:
102: return $template;
103: }
104:
105: }
106: