Zend Framework - When to use viewcripts / partials vs view helers

I am creating a display object library for our application. They display html of a large number of common objects (users, conversations, messages, etc.) in different views. By ideas, I mean that an object can drop different "zoom levels" with different markups.

Some display objects contain other display objects for rendering, for example. the user list object displays user objects in a specific form (this particular view returns them back to the list items so that they fit into the list)

I am trying to move them into the correct way to do something in ZF, but I cannot decide if they should all be viewers or all scripts / partial views.

Just forcing them to look at the scripts and rendering them using β†’ render () seems a little dirty because any information or parameters that I want to pass to them must be assigned to the view object.

Particles seem a little more correct, except that they are not sure that their implementation of the display logic in them is correct (if "showNotificationStatus" is passed as a parameter, draw this range). Or, if its kosher for partial displays other partial (a list of users representing the user object).

Looking at helpers seems like the right way to do this, but I don't know if this is abusing view helpers. Each object can be a view helper and take an objectview parameter so that it knows what zoom level / container to display itself, or each object can even be its own assistant (therefore there is no big switch statement inside the object). One nice thing about views is that you can pass parameters, and it still has access to the view context if you need something from this level.

Most of them are going to accept models, and some require some additional parameters in order to know what to do (for example, showNotificationStatus on top). What is the right tool for this?

+6
php zend-framework
source share
2 answers

One of the main ideas of partial actions is that they should be as usable as possible, so they have their own variable. I like to use partial files as pretty dumb containers for small HTML snippets. No core logic except a few if() or foreach() expressions.

If I need serious logic, I use an assistant. Assistants should be responsible for working with logic and calling visualization methods. I know that in the Rails world, helpers tend to encapsulate small pieces of logic, such as processing links or image tags. This is great, but I don’t think there is any harm in complicating them in ZF. I essentially use them to translate my business objects into representations.

I think the best way to use both partial and helpers is to help helpers set up the data and then transfer it to partial. Thus, your HTML remains very easy to maintain (and I suspect that every time someone dials echo "<a href='". $my_link . '"/>" kitten dies somewhere).

EDIT (specify):

What you need to remember about helpers is that you can treat them like normal classes, use a constructor with arguments, and have private members. Therefore, when you create an assistant, you can create a business object and then have several methods that display HTML (through partial).

So in my opinion:

 <?php $helper = $this->_helper->MyUserHelper($users); ?> <ul> <?php $helper->user_list(); ?> </ul> 

Here the user_list() method returns a collection of <li> elements with all the relevant data in them.

My assistant might look like this:

 class MyWidgetHelper { private $_widget; public function __construct($users) { $this->_users = $users; } public function user_list() { // do any necessary logic here // then return the html that gets rendered. // you can call a partial from here, and it just returns an HTML string. return $this->_view->partial('partials/_user_item.phtml', array('users' => $users) } } 
+15
source share

Helpers for logic (for example, converting an object to an array), partial for decorating logic (displaying a recursive array as a <ul> tree).

Remember that you can also use $this->render('anyfile.ext') or include() .

0
source share

All Articles