yii-view icon indicating copy to clipboard operation
yii-view copied to clipboard

Add specify a layout in injections

Open hacan359 opened this issue 1 year ago • 4 comments

What steps will reproduce the problem?

I have 2 layouts main and second. For main in the view, I need attributes to display, for example, the number of online users, the number of new users, etc. For the second layout I don’t need these parameters, but some others may be needed

What is the expected result?

I would like it to be possible in injections to specify the layout for which this injection needs to be connected.

What do you get instead?

Example

readonly final class CommonViewInjection implements CommonParametersInjectionInterface
{
    public function __construct(
        private ApplicationParameters $applicationParameters,
        private UrlGeneratorInterface $urlGenerator,
        private CurrentUser $user
    ) {
    }

    public function getCommonParameters(): array
    {
        return [
            'applicationParameters' => $this->applicationParameters,
            'urlGenerator' => $this->urlGenerator,
            'user' => $this->user,
        ];
    }

    public function includeLayout(): array
    {
      // return ['main', 'second']; // inject only for 'main', 'second' layout
        return ['*']; // wild for all layout OR empty
    }

    public function excludeLayout(): array
    {
        //case when i have more 30 layout and wont exclude this inject for only one
        return ['other']; // exclude layout for inject
    }
}

hacan359 avatar Apr 09 '24 17:04 hacan359

Isn't it better to create a separate injection set and use the one on demand?

xepozz avatar Apr 09 '24 18:04 xepozz

I can not use injections at all, but transfer everything from the controllers directly to the view. This problem has been resolved without modifications. I simply offered my version of how injections could be improved to make them even better. If this is the wrong approach or there is a problem with the implementation I suggested, you can do whatever implementation you need. For example, we’ve already done an implementation and it seems to me that it’s quite concise, even if it’s just a draft

hacan359 avatar Apr 10 '24 06:04 hacan359

Let's think, can be any layout specific settings except injections?

If no, #102 implementation is OK for me.

If yes, we can go to create abstract class (e. g. ParametrizedLayout), that provide methods return settings (injections and other). And user will be create own class with layout settings and pass it to ViewRenderer.

vjik avatar Apr 11 '24 19:04 vjik

At the moment, there are no other ideas, and the current implementation is the one needed.

hacan359 avatar Apr 12 '24 05:04 hacan359