This article mainly introduces the usage of Zend_View component in Zend Framework, and briefly analyzes the related skills and precautions of Zend_View component view operation in the form of examples. Friends in need can refer to it
The examples in this article describe Zend Framework introductory tutorial: usage of Zend_View component. Share it with everyone for your reference, the details are as follows:
The Zend_View component can realize the separation of the view part code from the Model and Controller part in the MVC mode.
Usage steps: First create a Zend_View instance in the Controller and pass the required variables to it; after that, the Controller notifies Zend_View to display a specific view,
Generate the content of View output.
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 (4- 2. Content in 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.
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to implement Ajax in the Zend Framework
Zend
Usage of Zend_Controller_Front in Framework
The above is the detailed content of Usage analysis of Zend_View component in Zend Framework. For more information, please follow other related articles on the PHP Chinese website!