search
Homephp教程PHP开发Zend Framework introductory tutorial - Zend_View component usage example

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools