【轉】Using partial helpers in Zend Framework

      在瞭解Zend Framework的Partial View Helper的過程中,Google了一篇相關的使用入門介紹,現轉貼於下。文中提供的

源碼下載,也上傳了一份。


      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.

發佈了96 篇原創文章 · 獲贊 1 · 訪問量 8059
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章