Using partial helpers in Zend Framework

Using partial helpers in Zend Framework

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:
1.you 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)); ?>
</div>

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)

<ul>
< ?php foreach($this->categories as $item) : ?>
	<li class="item">
	  <a href="<?php echo $this->url(
	  					array(
							"cat"=>$item['key'], 
                                                        "action"=>"index", 
	  						"controller"=>"index",
	  						"module"=>"default"
	  										))?>">
		< ?php echo $item['value']; ?></a>
    	</li>
< ?php endforeach; ?>
</ul>

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(
					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.
Enjoy.

Link to code files.

You made it all the way down here so you must have enjoyed this post! You may also like:

Using Redis cache backend and session storage in Magento Marko Martinovic
Marko Martinovic, | 43

Using Redis cache backend and session storage in Magento

Consuming Magento REST service using Zend_OAuth_Consumer Darko Goles
Darko Goles, | 45

Consuming Magento REST service using Zend_OAuth_Consumer

Unit testing with Zend framework: Setting up environment for the first test using Netbeans IDE for php Darko Goles
Darko Goles, | 1

Unit testing with Zend framework: Setting up environment for the first test using Netbeans IDE for php

9 comments

  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 😉

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>.

Tell us about your project

Drop us a line. We'd love to know more about your project.