In Zend Framework, Can anybody explain the difference between partial and placeholder?
From my understanding, one can render a particular template / container by using both placeholders and partials.
In which condition should one use a partial and what scenario is optimal use for placeholders?
It's pretty simple, the placholder is used to persist data between views and layouts and partials are used in specific views to do specific tasks.
See these excerpts from the reference.
77.4.1.6. Placeholder Helper: The Placeholder view helper is used to persist content between view scripts and view instances. It also offers some useful features such as aggregating content, capturing view script content for later use, and adding pre- and post-text to content (and custom separators for aggregated content).
77.4.1.5. Partial Helper: The Partial view helper is used to render a specified template within its own variable scope. The primary use is for reusable template fragments with which you do not need to worry about variable name clashes. Additionally, they allow you to specify partial view scripts from specific modules.
Here is a simple example of a placeholder, that sounds like what you want. (to persist data)
<?php
//this is placed in my layout above the html and uses an action helper
//so that a specific action is called, you could also use view helpers or partials
$this->layout()->nav = $this->action('render', 'menu', null,
array('menu' => $this->mainMenuId))
?>
<div id="nav">
//Here the placeholder is called in the layout
<?php echo $this->layout()->nav ?>
</div>
in this case the menu id's are setup in the bootstrap, however this is not requiered it just simple.
protected function _initMenus() {
$view = $this->getResource('view');
$view->mainMenuId = 4;
$view->adminMenuId = 5;
}
[EDIT]
I think a better placeholder example might be in order. This place holder is a small search form I use in several actions in several controllers in different configurations.
In this configuration this form is setup to search just for music artists, the controllers that use this placeholder will have different paths for setAction(), different Labels and sometimes different placeholder text. I use this same form to search music and video databases.
I you always use the same setup or have more interest in doing it then I do, this can be setup as a plugin.
//in the controller
public function preDispatch() {
//add form
$searchForm = new Application_Form_Search();
$searchForm->setAction('/admin/music/update');
$searchForm->query->setAttribs(array('placeholder' => 'Search for Artist',
'size' => 27,
));
$searchForm->search->setLabel('Find an Artist\'s work.');
$searchForm->setDecorators(array(
array('ViewScript', array(
'viewScript' => '_searchForm.phtml'
))
));
//assign form to placeholder
$this->_helper->layout()->search = $searchForm;
}
I use the placeholder in my layout (can also be used in any view script). The search form is rendered when the placeholder has a value and not rendered when the placeholder has no value.
//in the layout.phtml
<?php echo $this->layout()->search ?>
and just to be complete, here is the partial that the form uses as a viewscript decorator.
<article class="search">
<form action="<?php echo $this->element->getAction() ?>"
method="<?php echo $this->element->getMethod() ?>">
<table>
<tr>
<th><?php echo $this->element->query->renderLabel() ?></th>
</tr>
<tr>
<td><?php echo $this->element->query->renderViewHelper() ?></td>
</tr>
<tr>
<td><?php echo $this->element->search ?></td>
</tr>
</table>
</form>
</article>
This example should really illustrate the difference between partial and placeholder.
I think what might be more helpful for you here is either a custom view helper, possibly expanding on the existing Zend_View_Helper_FormSelect class or create a custom Zend Form element that suits your needs. Alternatively, a helper script that's in a general location may be the best bet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With