UI\Control::$templateFile

13 days ago

holantomas
Backer | 56
+
0
-

new feature
BC break: no(for 3rd party controls maybe)
doc PR: not needed

Hi everyone,
did you look on componette for Controls? There're alot of which using this

  1. Direct control $templateFile property
  2. Some kind of BaseControl which expand UI\Control with $templateFile property

Look at contributte/datagrid or iPublikuj/visual-paginator for example.

So I did little modification in UI\Control and add this to it. What you say? Should it be in Nette or every user should define it by him self?

First, this is workaround for setup template file immediately after create control(or in constructor) by setter. if author implement it in own Control right way, this give to his user ability to change default template. This all no matter if template is already created and without unnecessary template creating.

Second, this isn't BC break for Nette and should not be for 3rd party controls(but who knows?).

Example:

// Presenter
protected function createComponentDatagrid() {
    $grid = new DataGrid();
    $grid->setTemplateFile('/my/modified/template/file.latte');
    // $grid->template === null
    // So we did setup template file but don't know if component will be rendered and template wasn't create yet.
    return $grid;
}

// DataGrid - first way
public function render() {
    $this->getTemplate()->setFile($this->getTemplateFile() ?? self::DEFAULT_TEMPLATE_FILE);
    $this->getTemplate()->render();
}

// DataGrid - second way
public function __construct() {
    $this->setTemplateFile(self::DEFAULT_TEMPLATE_FILE);
}

public function render() {
    $this->getTemplate()->setFile($this->getTemplateFile());
    $this->getTemplate()->render();
}

Here, is modification prepared for PR.