简体   繁体   中英

Zend 2 challenge :How to configure Zend Framework 2 to hold generic template outside a module?

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 (ie header(contains menus etc) ,footer,head (contains css/javascripts)) so that i could use them for other modules.See screen shot for clarification: 在此处输入图片说明

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.(eg 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

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,
            )
        )
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM