Using partial helpers in Zend Framework

Featured Image

ZendFrameworkQuickstart application is demo guestbook with Zend library files included,
basic folder structure and some sample data for fetching and entering new.
It is already configured for usage and always updated with the latest release of Zend Framework.

The quickest way to setup Zend FrameworkQuickstart app for you is next 2 steps: need to download ZendFrameworkQuickstart application files
2.and setup virtual host pointing to public folder (e.g. /var/www/zend.quickstart/public/)

I used ZendFrameworkQuickstart application for you so you can easier test the code I provided.
You just need to download and setup latest ZendFrameworkQuickstart and replace default files with files I posted.
Here is the post:

In layout file (our case “layout.phtml” in application/layouts/scripts/)
we just need to call helper $this->partial and correct path to partial file

<div id="categories">
    < ?php echo $this->partial('partials/categories.phtml', 
				            array('categories' => $this->data)); ?>

with correct “keys” and “values” (our case ‘categories’ and $this->data).
Key (‘categories’) will stand for entity which is iterrated in our partial file in foreach loop
(our case “categories.phtml” in application/layouts/scripts/partials)

< ?php foreach($this->categories as $item) : ?>
	<li class="item">
	  <a href="<?php echo $this->url(
		< ?php echo $item['value']; ?></a>
< ?php endforeach; ?>

and value ($this->data) must be defined in our controller class file as a view helper ($this->view->data),
I defined it in init() method so it will be set for all actions in IndexController.

public function init()
		$this->view->data = 
					array('key' => 'key_1', 'value' => 'category_1'),
					array('key' => 'key_2', 'value' => 'category_2'),
					array('key' => 'key_3', 'value' => 'category_3'),
					array('key' => 'key_4', 'value' => 'category_4'),

I added demoAction so you can try to set different data set or you can try to play with the code and create some data set from query result.

Link to code files.


  1. Nice and still I could not understand it. I am a newbie in zend so i did not knew where to place all of this 😐 if you could still give me a hand of help..that will be great, thanks 😉

  2. Cool work. It really helped me a lot. Thanks for sucha nice demonstration of the code.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.