Why is " Nette\Bridges\ApplicationLatte\DefaultTemplate" final?

Zrny
Member | 5
+
0
-

I was just watching this video: https://www.youtube.com/watch?…

Around 16:00 David talks about a class, that can be used as a something like “variable holder” for a template to get auto-completing in both latte and a $this->template->.

I like the idea of using Nette/SmartObject to make everything strict, but i still would like to use those “magic” variables like $basePath.

Because the DefaultTemplate class is final, i cannot extend my HomepageTemplate with DefaultTemplate and SmartObject.

The only solution i see is to create my own DefaultTemplate that would actually be almost same as the Nette\Bridges\ApplicationLatte\DefaultTemplate class. Right?

But why? Why i cant just extend the original DefaultTemplate so i don't have to have 2 similar classes?

Or is there something wrong with what i am thinking about?

David Grudl
Nette Core | 8082
+
0
-

The only solution i see is to create my own DefaultTemplate

Yes, your template will extend Template, not DefaultTemplate.

that would actually be almost same as the Nette\Bridges\ApplicationLatte\DefaultTemplate class. Right?

No. You only define variables you really need. You probably only use $basePath and $user, right?

Zrny
Member | 5
+
0
-

Thanks for joining me here in this topic,

my point is:

  • We can have a layout that uses (as you wrote) $basePath & $user (and also $flashes).
  • We need to provide those variables to the template.
  • If each *Presenter has its own *Template, i need to have some kind of BasePresenter & BaseTemplate to hold those variables, and every template & presenter needs to extend those base ones.
    • sub question: do i have to create another template file next to presenter or can i somehow disable phpstorm inspections to not scream about 2 classes in one file?
  • Now: As i seen in another talk and i am already implementing it:
    • My presenters are extending Nette\Application\UI\Presenter only.
    • If i have a behavior that is required by more classes, i use traits with inject methods. For example:
      • use Traits\Translated;
      • use Traits\Layouts\Admin;
      • use Traits\RequireAdminPrivileges;
  • This sets the trend that my structure in Presenters directory contains only actual presenters that users can reach and Templates for every single presenter.
  • If i want to not repeat my self and define those three variables in every single one template, i have 2 options:
    • Create BasePresenter, that will hold $user and the other ones, and every presenter will need to extend it.
    • Create LayoutTemplate near BasePresenter or, if i dont have BasePresenter, i need to think of some better place in the structure for this kind of file(s).

The problems i have with both cases are:

  • I don't want to use BasePresenter
  • I don't want a class BaseTemplate in Presenters directory or anywhere else.
  • Why should i create a class, that already exists.

So, for my use-case, the single one keyword final means 1 or 2 additional classes and one more thing to maintain if something changes in the Nette\Application => Latte bridge.

Someone might say Meh, whatever... to this, but not my OCD :D

I don't insist on implementing this change, i just want to clear my point why it makes me curious. I am not implementing presenter templates yet, but i would love to have only one line in my template, that will define all the custom variables for phpstorm auto-complete. Now i need to add varType for the non-standard ones.

As you wrote:

You only define variables you really need.

That makes sense, but saying:

Why would you need $control in your template when you only use $basePath. Just define your own class that only has $basePath.

is like saying:

Why would you need package with method Strings::webalize() and many more in Utils package when in only use Strings::contains(). Just define your own class contains() method.

Hope you understand what i mean. It's so clear in my head, but its so hard to verbalize it. Even when my english is not perfect, i woudn't be able to do it better in czech anyway.

Thanks for your time.