# Specific path to view template with condition

**URL:** <https://discourse.laminas.dev/t/specific-path-to-view-template-with-condition/2207>\
**Category:** Components & MVC\
**Tags:** laminas-mvc, laminas-view\
**Created:** [March 26, 2021, 8:34am UTC](https://discourse.laminas.dev/t/specific-path-to-view-template-with-condition/2207 "2021-03-26T08:34:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kliker02](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kliker02](https://discourse.laminas.dev/u/kliker02)\
**Post date:** [March 26, 2021, 8:34am UTC](https://discourse.laminas.dev/t/specific-path-to-view-template-with-condition/2207/1 "2021-03-26T08:34:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 26, 2021, 9:28am UTC](https://discourse.laminas.dev/t/specific-path-to-view-template-with-condition/2207/2 "2021-03-26T09:28:03Z")

</div>

> [@kliker02](#):
>
> I tried to attach `render` event and getting ViewModel for changing template path but when i called

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/](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:

```php
/** @var ViewModel|null $contentViewModel */
$contentViewModel = (array_filter(
        $event->getViewModel()->getChildren(),
        static fn(ViewModel $child) => $child->captureTo() === 'content'
    ))[0] ?? null;

if ($contentViewModel) {
    $contentViewModel->setTemplate('…');
}

```
