Distrust wrote
> I want to setup the following skin directory structure:
> 
> module/Application/view/
>    - default
>        - application
>            - index
>                - index.phtml
>            - test
>                - index.phtml
>                - something-else.phtml
>    - blue
>        - application
>            - index
>                - index.phtml
>            - test
>                - index.phtml
>                - something-else.phtml
>    - red
>        - application
>            - index
>                - index.phtml
>            - test
>                - index.phtml
>                - something-else.phtml
> 
> I know that I can change template in this way:
> $viewModel->setTemplate($templatePath);
> but when I try to change template to
> "/blue/application/test/something-else" in the init() method in the Module
> class then I'm getting for example this error:
> Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException'
> with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render
> template "application/test/something-else"; resolver could not resolve to
> a file'.
> 
> My code:
> // ...
> 
> class Module
> {
>     // ...
> 
>     public function init(ModuleManager $moduleManager)
>     {
>         $sharedEvents =
> $moduleManager->getEventManager()->getSharedManager();
>         $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
> 
>             $controller = $e->getTarget();
> 
>             $skinName = 'blue';
>             $templatePath = $skinName .
> '/application/test/something-else';
> 
>             $viewModel = $e->getViewModel();
>             $viewModel->setTemplate($templatePath);
> 
>         }, 100);
>     }
> }
> 
> What I'm doing wrong?
> I also tried to use setLayout() method, but with similar result.

Hi, setTemplate and setLayout functions change the default layout template
that is being used. Clearly what you're trying to achieve here is to change
default views path and you can easily do so by overriding your
template_path_stack. Try this:

class Module
{
    // ...

    public function init(ModuleManager $moduleManager)
    {
        $sharedEvents =
$moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            $serviceManager = $e->getApplication()->getServiceManager();
            $templatePathResolver =
$serviceManager->get('Zend\View\Resolver\TemplatePathStack');
            $templatePathResolver->setPaths(array(__DIR__ . '/view/blue'));
// here is your skin name

        }, 100);
    }
}



-----
Cheers,
--
Luke Mierzwa
--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/ZF2-How-to-change-template-directory-tp4656995p4657052.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to