Latte: higher abstraction for Application\UI

6 years ago

David Grudl
Nette Core | 6864
+
0
-

(This builds upon the previous RFC).

Firstly, the Nette\Templating***Template becomes very humble Nette\Application\UI\Template and will be used internally by Control (in fact for backward compatibility, or like abstraction for different templating engines):

class Template
{
    private $engine, $file, $params;

    function __get() // for $params
    function __set()

    function setFile($file) { // for compatiblity
        $this->file = $file;
    }

    function registerHelper(...) { // for compatiblity
        $this->latte->addModifier(...);
    }

    function render($file = NULL) {
        $this->engine->render($file ?: $this->file, $this->params);
    }
}

Secondly, Template will be created by Application\UI\TemplateFactory, which will configure Latte for Application\UI (it means: appends UI's and form's macros, sets HTML\XHTML mode and all required parameters like _presenter or $basePath).

TemplateFactory, as service, will be passed via DI to presenters.

All controls will use the presenter's TemplateFactory by default:

abstract class Control extends PresenterComponent implements IRenderable
{
    protected function createTemplate()
    {
        return $this->getTemplateFactory()->create($this);
    }

When a control will need add or reconfigure some macros, it can overwrite createTemplate or add them in render().

When a control will need use different Latte factory, it overwrites getTemplateFactory() (and returns different factory, which receives via constructor etc.).

NOTE! You can pass custom Template factory to Presenter too. But no control shall rely on this customization. All controls must be renderable with the default factory OR must overwrite getTemplateFactory and use its own factory.

5 years ago

David Grudl
Nette Core | 6864
+
0
-

Done in v2.2