Hello!
I have in my component 3 users roles: admin, client, guest.
I want that template is displayed depending on user role:
so if user have ‘admin’ role, than template view path will following:
view/admin/module/controller/action.phtml
and same for other roles!
I tried to attach render
event and getting ViewModel for changing template path but when i called
$ViewModel->getTemplate()
returned empty string
What should i do for customize path to view template by some variable - identity role in this case?
In the laminas-view documentation you will find an example of working with a listener:
https://docs.laminas.dev/laminas-view/cookbook/setting-module-specific-layouts/
In your case, add the authentication service (Laminas\Authentication\AuthenticationServiceInterface
) to the listener via constructor and then you must set the template for the correct view model:
/** @var ViewModel|null $contentViewModel */
$contentViewModel = (array_filter(
$event->getViewModel()->getChildren(),
static fn(ViewModel $child) => $child->captureTo() === 'content'
))[0] ?? null;
if ($contentViewModel) {
$contentViewModel->setTemplate('…');
}