简体   繁体   中英

How to reuse methods in ZF views

In my application i have the following classic template on 3 columns

*********************************************************
*                                                       *
*                                                       *
*********************************************************
*        *                               *              *
*        *                               *              *
* sidea  *         content               *    sideb     *
*        *                               *              *
*        *                               *              *
*        *                               *              *
*        *                               *              *
*        *                               *              *
*        *                               *              *
*********************************************************
*                                                       *
*                                                       *
*********************************************************

Where the header and sideb are similar for the most part of the site (side b contents depends on the user's state so they need to be rendered dynamicaly), and sidea & content changing for every controller.

Since I'm new to ZF (1.9), I have some difficulties to implement such setting, where script views reuse common methods (for the sideb), and there is 3 different places for dynamic contents injection. Can somebody hint any tutorial/code example?

Thanks in advance.

Edit

After some more deeper reader of zend_layout documentation and some other links (ie this post ) i found out that i should concentrate my research in direction of Composite View pattern for zend framework, in particular Zend_View_helper_partial. Probably following two links can give me a hand (and to other folks searching the same thing)

Link 1 Link 2

Looks like you need a layout. Have a look at the Zend_Layout doc page and see if that achieves what you want.

Check out this webinar for info on what's possible with Zend_Layouts

View Helpers are useful for functions that get reused.

http://framework.zend.com/manual/en/zend.view.helpers.html

Use yout view script only for rendering the content of the page.

Utilize Zend_Layout to render the rest of the page and call the view helpers from your layout to populate your left and right sidebars.

If your 'sidea' is tied to your current action/controller you can render them from the actions view script and call the 'sideb' view helper from the layout.

You can of course render different parts of your view script to different response segments and use the layout file to put the response segment where you want it to be.

Forwarding to multiple controllers/actions is as Alekc wrote a huge impact on the performance of your application and is is quite unnecessary in this situation.

I'll echo that what you're looking for is a layout. With the layout you can have a controller render a view into different 'content' areas.

Combining this with the ability to 'forward' rom one controller to another, the main controller for a request could render a view (or multiple views) into the content placeholder, then forward to controllers that render (if needed) the sidebars/header/footer.

For views that don't require those elements (say the 'print' action for an article controller), you could just disable the layout, and render the view normally (without any forwarding to other controllers).

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