Home >php教程 >PHP开发 >Zend Framework introductory tutorial - Zend_View component usage example

Zend Framework introductory tutorial - Zend_View component usage example

高洛峰
高洛峰Original
2017-01-05 10:22:171432browse

The example in this article describes the usage of the Zend_View component in the Zend Framework introductory tutorial. Share it with everyone for your reference, the details are as follows:

The Zend_View component can realize the separation of the code of the view part from the Model and Controller parts in the MVC mode.

Usage steps: First create a Zend_View instance in the Controller and pass the required variables to it; then, the Controller notifies Zend_View to display a specific view and

generates the content output by the View.

Instance controller code script:

<?php
//使用模型来获取书籍作者和标题相关数据
$data = array(
  array(
    &#39;author&#39;=>&#39;曹雪芹&#39;,
    &#39;title&#39;=>&#39;红楼梦&#39;
  ),
  array(
    &#39;author&#39;=>&#39;罗贯中&#39;,
    &#39;title&#39;=>&#39;三国演义&#39;
  ),
  array(
    &#39;author&#39;=>&#39;吴承恩&#39;,
    &#39;title&#39;=>&#39;西游记&#39;
  ),
  array(
    &#39;author&#39;=>&#39;施耐庵&#39;,
    &#39;title&#39;=>&#39;水浒传&#39;
  )
);
//传递数据给Zend_View类的实例
require_once &#39;Zend/Loader.php&#39;;
Zend_Loader::loadClass(&#39;Zend_View&#39;);
$view = new Zend_View();
$view->books = $data;
$view->setScriptPath(&#39;./&#39;);
echo $view->render(&#39;4-2.php&#39;);

View script code (content in 4-2.php):

<?php if($this->books):?>
<table>
  <tr>
    <th>作者</th>
    <th>标题</th>
  </tr>
  <?php foreach($this->books as $key=>$val):?>
  <tr>
    <td><?php echo $this->escape($val[&#39;author&#39;])?></td>
    <td><?php echo $this->escape($val[&#39;title&#39;])?></td>
  </tr>
  <?php endforeach;?>
  </table>
<?php else:?>
<p>没有需要的书目</p>
<?php endif;?>

Execution result:

作者   标题
曹雪芹   红楼梦
罗贯中   三国演义
吴承恩   西游记
施耐庵   水浒传

Summary (practical experience):

This case does not have a big structure. Zend_View is equivalent to a plug-in and can be called flexibly. The content in the Zend framework does not necessarily need to be used in a large architecture.

MVC is not so rigid, it is just an idea. The M layer here is a set of arrays. In the actual development process, information should be read from the database. The view layer directory is set to the current directory through $view->setScriptPath('./');.

In this way, you can directly call the file 4-2.php in the current directory. After the view layer receives the data, it performs certain processing.

The main function of Zend_View is to pass data from the controller layer to the view layer.

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

For more Zend Framework introductory tutorials and Zend_View component usage examples, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn