Passing config paramater to all presenters

6 years ago

Etruska
Member | 25
+
0
-

Hi,
I would like to pass parameter(s) from configuration to <head> section in layout so it probably has to be passed to all front-end presenters. I guess I could make a service which only purpose would be to carry the parameter and inject the service to BasePresenter. However I don't find such service very useful and also I've read some bad things about using BasePresenter. What would you recommend? Thank you for your advice.

6 years ago

looky
Member | 100
+
0
-

Base presenter is fine, and so is your solution, so I'd say go ahead…

6 years ago

Tomáš Votruba
Moderator | 1154
+
0
-

Hi, you could make a component {control head}.

Ad BasePresenter: I prefer using traits which I pass to presenter. E.g. TWebloaderComponents is more clear than BasePresenter.

6 years ago

Etruska
Member | 25
+
0
-

@**Tomáš Votruba**: I must admit traits are fairly new to me. It does look better then BasePresenter, I'll give it a try :).

Ad component: Component makes sense but I would still have to pass config parameters via service, right? Or am I missing something?

6 years ago

Tomáš Votruba
Moderator | 1154
+
0
-

You can pass it directly to component.

services:
    -
        implement: IHeadComponent
        setup:
            - setParameters(@Nette\DI\Container::parameters)

It's called generated factories, see this article (in czech)

6 years ago

Etruska
Member | 25
+
0
-

I didn't know about this feature, it's really neat :). Thank you.