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.

8
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Vedran Subotic

Ex. Inchooer

Vedran worked in Inchoo from 2009 to 2012 as a Senior backend developer and as a team leader.

Other posts from this author

Discussion 8 Comments

Add Comment
  1. Welcome Vedran and congratulations for your first article.

  2. Good one dude .

    Thanks

  3. Jthu Mohandas

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

  4. quangtruong1985

    Great job dude. It helps me out of headaches.

  5. 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 ;)

  6. Thanks, that was so useful

  7. Typo in Vedran’s profile: “Magneto” ==> Magento.

  8. @Niels great catch but I think it’s not a typo, maybe it’s intentional ;)

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top