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( 'author'=>'曹雪芹', 'title'=>'红楼梦' ), array( 'author'=>'罗贯中', 'title'=>'三国演义' ), array( 'author'=>'吴承恩', 'title'=>'西游记' ), array( 'author'=>'施耐庵', 'title'=>'水浒传' ) ); //传递数据给Zend_View类的实例 require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_View'); $view = new Zend_View(); $view->books = $data; $view->setScriptPath('./'); echo $view->render('4-2.php');
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['author'])?></td> <td><?php echo $this->escape($val['title'])?></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!