0

I am new to ZF 2 and just trying to render generic layout outside a module's view directory.

At the moment ,i have a module "categories" and in "Categories\view\generic\" ,i have splitted all common layout elements (i.e header(contains menus etc) ,footer,head (contains css/javascripts)) so that i could use them for other modules.See screen shot for clarification: enter image description here

Instead of creating same layout(header,footer,menus) for each and every module again and again ,i want to make these layout /views accessible for all modules.(e.g Users,categories modules should have same footer,header all the time). My question is :

1- As a best practice, in my case Where should i place my generic layout files to make them accessible for all modules within a project?In general,generic layout shouldn't reside within a specific module's view directory.

2-Where and What changes i would need to make to my configuration files to make them work?

I have tried to play with Evan's EdpModuleLayout

(https://github.com/EvanDotPro/EdpModuleLayouts/)

and also tried

" Set a generic layout for all modules in Zend framework 2 "

But couldn't succeed.I would appreciate if anyone could guide me in this regard. Thanks

Community
  • 1
  • 1
9 Digit
  • 167
  • 2
  • 11
  • Views are accessible for all modules by default, you don't need to do anything special. What error are you currently getting? – Tim Fountain Nov 03 '14 at 16:24
  • I know views are accessible but want to create a separate repository for generic layouts outside a specific module' scope..is it possible? – 9 Digit Nov 03 '14 at 16:30
  • 2
    Sure, just create a separate module that only contains your generic views/layouts. – Tim Fountain Nov 03 '14 at 16:36
  • I have created a separate module but Zend renderer is not find a path,giving me warning include(C:\wamp\www\Wex\module\Users\config/../view/layout/layout.phtml): failed to open stream: No such file or directory in C:\wamp\www\Wex\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php" – 9 Digit Nov 04 '14 at 15:35
  • What code are you using that generates that error? – Tim Fountain Nov 04 '14 at 17:07
  • i created an empty Layout module and then added path of my layouts' views to my individual modules module.config.php i.e 'template_map' => array( 'layout/layout' => __DIR__ . '/Layouts/view/layout/layout.phtml', 'categories/index/categories' => __DIR__ . '/../view/categories/categories/index.phtml', 'error/404' => __DIR__ . '/Layouts/view/error/404.phtml', 'error/index' => __DIR__ . '/Layouts/view/error/index.phtml', ), – 9 Digit Nov 04 '14 at 18:58
  • Put your layout configuration in the layouts module's config file. Would still need to see what code is causing the error you posted. – Tim Fountain Nov 04 '14 at 20:51

1 Answers1

1

ZF2 already has a generic shared layout for all modules. the default is layout/layout.phtml. just put this file in the Application modules view directory and it will be shared for all modules.

all view scripts from all the loaded modules can be used in other modules, u just need to provide a template path for the view model:

$viewModel->setTemplate('application/index/index');

these are the default values:

$viewModel->setTemplate('[module namespace]/[controller]/[action]');

but when u provide the template path manually they can be anything in the registered view directories.

you can also change the default layout file path in view manager config :

    'view_manager' => array(
        'display_not_found_reason' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
//        'layout' => 'layout/layout.phtml',//<----------------------------
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
        'strategies' => array(
            'ViewJsonStrategy',
        ),
    ),

or in the controller itself :

 $this->layout('layout/layout.phtml');

or in a event if you need it to be dynamic.

to define another directory for you view files you need to change/or add to the view manager's config

'template_path_stack' => array(
    __DIR__ . '/../view',
),

or in e event if you need it to be dynamic :

$templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack');
    $templatePathResolver->setOptions(
        array(
            'script_paths' => array(
                $client_theme_path,
            )
        )
    );
Exlord
  • 5,009
  • 4
  • 31
  • 51