Zend Framework: what is the difference between partials and placeholders - zend-framework

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.

Related

How to integrate a non-model radio button element to a CActiveForm in Yii?

In a Yii application, I'm working on, there is a simple CActiveForm, where all elements are model related and look like this:
<div class="row">
<?php echo $form->labelEx($model, 'foo'); ?>
<?php echo $form->textField($model, 'foo', array(/* HTML options */)); ?>
<?php echo $form->error($model, 'foo'); ?>
</div>
Now I want to implement following case:
The should be a radio button element bar with two options. Depending on the selected option different form elements should be displayed (a dropdown list with existing bar-entries oder a set of field to create a new one). In the controller I want to analyze the field bar and, if needed (means e.g. if bar == 1) use the provided field set to create a new table entry.
CActiveForm#radioButton(...) needs a model as input. But for this case the model is not relevant -- the radio button I need should only contain/provide the information, how the data has to be processed. Is there a Yii conform way to implement this requirement and create a "non-model" form element for a Yii CActiveForm?

apply class active to anchor tag in Zend_Navigation

i am using partials to decorate Zend_Navigation to fetch desired output. everything is working good except i am having difficulty adding class="active" in <a href..> tag. here is my layout partial sidebar.phtml
<ul id="menu" class="nav">
<?php foreach($this->container as $page): ?>
<li class="<?php echo $page->class; ?>"><span><?php echo $page->label; ?></span></li>
<?php endforeach; ?>
</ul>
in the <a href="<?php echo $page->getHref(); ?>"> i want to add class="active" for the current page.
i tried some solutions which i found after searching. but nothing worked for me.
most of the solutions talk about doing it in controller for example
$page = $this->view->navigation()->findOneByLabel('Your Label');
if ( $page ) {
$page->setActive();
}
i haven't tested this yet. as i am using multiple navigations in the layout from one single navigation.xml file. i was wondering if is there a way i could do it in partials itself instead of in controllers or other helpers?
thank you
It's easy. In the view partial you can check which page is active:
if ($page->isActive()) { ... }
From ZF documentation:
Note: Note that when using the route property in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.

How can I dynamically create multiple checkboxes with Zend Form?

Could you please help me to solve my problem?
I would like to place a checkbox on every row of a list of users built with database information and add a validation button to post my form. My list should look something like this:-
So the user will select the checkbox linked to the student he wants to validate.
The number of rows of the result is variable, so i don't know how to do it.
I hope i've been clear in my description.
You haven't shown any code, but I am assuming that you get the number of students somewhere in your controller.
To achieve what you want with Zend_Form, you will need to render each element individually, but first you need to find a way of adding the correct number of elements to your form.
Preferably, you would do this in your form class to keep the logic out of the controller, but to keep this answer simple I will show you how to achieve this in your controller, you can then adapt the code as you wish.
$numStudents = getNumberOfStudentsSomehow();
$studentForm = new yourFormClass();
for($i = 0; $i <= $numStudents; $i++){
$checkBoxes[] = new Zend_Form_Element_Checkbox('checkBox_' . $i);
}
$studentForm->addElements($checkBoxes);
$this->view->studentForm = $studentForm;
Your form now has the correct number of check boxes in it and you can pass it to the view.
In the view you have several options for rendering the form, either a view partial as suggested by RockyFord, a view helper (documentation here), create a custom view script for your form, or render directly in your view.
To get you started you can render individual elements from your form in your view like this:-
echo $this->view->studentForm->checkBox_0;
I think this might be a situation were a partialLoop() might be the best solution.
in your controller get your data from the model as usual and assign it the data to the view
$this->view->modelData= $data;
next make a new .phtml file in /views/scripts for this demo we'll call it _demoRow.phtml then code the html and php for one table row (in this case).
<tr>
<td><?php echo $this->name ?></td>
<td><?php echo $this->class ?></td>
<td><?php echo $this->birth_date ?></td>
<td><input type="checkbox" name="id" value="<?php echo $this->id ?> /></td>
</tr>
Then in your normal view just put the static information and render the partial
<form action="/your/action/url" method="post">
<table class="spreadsheet" cellspacing="0">
<tr>
<th>Student Name</th>
<th>Class</th>
<th>Birth Date</th>
<th>Select</th>
</tr>
<?php echo $this->partialLoop('_demoRow.phtml', $this->modelData) ?>
<tr>
<input type="submit" name="submit" value="valider" />
</tr>
</table>
</form>
This should approximate what you're looking.

Html coders + zend form

So I use a zend framework, but for all commercial projects I am not using zend form component.
HTML+Js coders are working on frontend and I don't want to have nothing with it, I just connect the final view in my zf based application.
So for example I have in view a html form which looks like this:
http://pastie.org/1668143
So form design, elements and classes can be changed by frontend coders, and I want to know is there easy way for me, to use zend form with this concept (in each project form code looks different of course )?
My main problem here is code duplication in my controllers, which in this case looks something like this:
http://pastie.org/1668023 (please don't take exceptions and loggedMember seriously, they are just used for this snippet, to demonstrate a problem, which I have)
So, what would be the best solution for problem which I have, what do u think :) ?
If you have absolutely no control over the form's html structure, but still want to maximize the use of Zend_Form's features, use Zend_Form_Decorator_ViewScript.
http://framework.zend.com/manual/en/zend.form.standardDecorators.html (last section)
$element->setDecorators(array(array('ViewScript', array(
'viewScript' => '_element.phtml',
'class' => 'form element'
))));
I would do it like this:
create a form class that has all elements, validators and filters
create an instance of the form in your action and set the view script(s) (this way you can change them per controller and still have very little duplicated definition code.
Splendid, I don't understand why you would have a problem with code duplication, in your 2nd link, you are performing your checks, then check if the page is a post request, then performing the checks again, yes its duplicated, but I don't understand what you are trying to explain by doing this?
As for the form, its up to you how you use it, you could create the form object, then instead of ever out putting the form, simply pass it the data from your designers form, and use it to validate things.
Or you could use custom templates for the form, OK it means you don't give the designers quite as much freedom of them designing a form and you sorting the results, but they can still do their best at it.
This is the setup I use, after all I am in charge of the functionality as the programmer, the designers just make it look good what the user see's.
So for example, if I want to create an input element I can:
$arrival_time = $this->createElement('text', 'arrival_time', array(
'required' => true,
'label' => 'Arrival Time:',
));
$arrival_time->removeDecorator('HtmlTag');
$this->addElement($arrival_time);
Notice I have removed the HtmlTag decorators here - I don't need them for the markup as the designers will be arranging things for me.
Next thing to do is tell the form to use the layout the designers have made:
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'NewArrivalsForm.phtml'))));
Here my template is within the view's, script's directory.
Now the designers have a few options. They could do:
<table border="0" width="100%">
<tr>
<td>
<?php echo $this->element->arrival_time; ?>
</td>
This will give you the following output:
<td>
<dt id="arrival_time"><label for="arrival_time" class="required">Arrival Time:</label></dt>
<input type="text" name="arrival_time" id="arrival_time" value="" />
</td>
If there we're an error, that would be presented as well. You could remove the decorators 'Label', 'Description' & 'Errors' as well, to make it simply an input box:
<td>
<input type="text" name="arrival_time" id="arrival_time" value="" />
</td>
Even once you have removed the decorators, the designers could still use for example:
<tr>
<td>
<?php echo $this->element->time_on_site->getLabel(); ?>
</td>
</tr>
<tr>
<td>
<?php echo $this->element->time_on_site ?>
</td>
This will allow them to lay the form out exactly as they want to. But it will still allow you to use the full power of Zend_Form for your final validation checks. Take a look inside the Zend/form/element.php for all the methods you and your designers can use on the form element.

porting template to zend framework

I got few issues proting a pear based form to zend form.
I have few elements I need :
Basic Elements
Groups
Group Elements
Sections
I previously used templates to render the forms on Pear. I obviously cannot use pre-existing zend decorators, since I need to specify css classes for each of the components of my base elements.
To see the issue I need to render this, which is the template for a basic element :
<li class = "{position_in_the_form} {error}">
<label class="{label_class}"> {label}
[<span class="required_class"> * </span>]
</label>
<div> {element_content} </div>
[<p class = "{error_class}"> {error_message} </p>]
</li>
So as you can see I have many dynamic things I would like to be able to specify : position in the form, class for the label, class for the required section, the class for the error.
I would also like to be able to specify this from an ini file. I manage to set up the basic meta from the ini but not custom fields.
One of the reason I cannot use basic decorators is that I need to have "error" in the "li" class when there is an error in the element or the sub_form.I'm not sure this is possible with the error decorator... (correct me if I'm wrong)
Also, for the group I need something handling the errors, and since the core groups don't handle errors I need to subclass the sub_form. But how can I create a subform in an ini file and I don't know how to provide parameters to the sub form fromn the ini.
The main idea here is to be able to have visual and logic groups of elements in a form. For example I need a 'name' group with fullname, middle name, etc. This also implies a global validator for this "name" group.
An other thing is that I want to be able to position these groups : left half, right half, full
I got the css ready for this and working with pear.
So what I need is a simple solution, with few code and ini configurations. Unfortunately I think I got stuck in something too complicated, so if someone has any idea about a simple architecture it would be amazing!
Thanks in advance for your help,
Best, Boris
In your complex decoration need, you might want to use the ViewScript Zend_Form_Element_Decorator
$element->setDecorators(array(
array('ViewScript', array('viewScript' => 'path/to/your/views/element.phtml')),
));
and then in path/to/your/views/element.phtml, more or less something like
<li class="<?php echo $this->element->getAttrib('position_in_the_form') ?> <?php echo $this->element->hasErrors() ? 'error' : '' ?>">
<label class="<?php echo $this->element->getAttrib('label_class') ?>">
<?php echo $this->formLabel($this->element->getName(),
$this->element->getLabel()) ?>
<? if ( $this->element->isRequired() ) { ?>
[<span class="required_class"> * </span>]
<? } ?>
</label>
<div> <?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?> </div>
<? if ( $this->element->hasErrors() ) { ?>
[<p class="<?php echo $this->element->getAttrib('error_class') ?>"> <?php echo $this->formErrors($this->element->getMessages()) ?> </p>]
<? } ?>
</li>
This is only a drafty snippet of code, but should lead you in the direction you aim.
Regards