Home  >  Article  >  Backend Development  >  ThinkPHP's method of dynamically including files, _PHP tutorial

ThinkPHP's method of dynamically including files, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:48736browse

ThinkPHP’s method of dynamically including files,

The example in this article describes the method of dynamically including files in ThinkPHP. Share it with everyone for your reference. The specific analysis is as follows:

Problem description: It is also a very common problem to encounter problems when working on projects. Generally, the homepage contains headers and steps. In order to facilitate management, these need to be implemented using include files. ThinkPHP provides the method of including files. The above is the simplest way to include the operation, but during the running process, I found that when requesting, only the template file is requested, which is the so-called static inclusion, but it is difficult if the menu is dynamically generated. Done.

Find a solution online: use Widget

1. We implement a classified display Widget on the page. First we need to implement it. We first define a Widget controller layer CateWidget, as follows:

Copy code The code is as follows:
class CateWidget extends Action {
Public function menu(){
           return 'menuWidget';                                 }  
}
Note that it is defined in the Widget package, which is different from the general Action
2. Then, we call this Widget through the R method in the template (the extended Widget method uses the W method in the template). If you don’t know about the R function, please refer here. (http://www.thinkphp. cn/info/134.html)

{:R('Cate/Menu','','Widget')}

The output after execution is: menuWidget

3. If the menu method of the CateWidget class is changed to:


Copy code The code is as follows:
class CateWidget extends Action {
Public function menu(){
echo 'menuWidget';
}  
}
4. The usage in the template needs to be changed to:

Copy code The code is as follows:
{~R('Cate/Menu','','Widget')}
5. If you need to use parameters when calling Widget, you can define it like this:


Copy code The code is as follows:
class CateWidget extends Action {
Public function menu($id,$name){
echo $id.':'.$name;
}  
}
6. To call parameters in the template, use:

Copy code The code is as follows:
{:R('Cate/Menu',array(5,'thinkphp'),'Widget')}
will output 5:thinkphp

7. Let’s take a more complicated example:


Copy code The code is as follows:
class CateWidget extends Action {
Public function menu(){
$menu = M('Cate')->getField('id,title');
           $this->assign('menu',$menu);                                                 $this->display('Cate:menu');                                 }  
}

8. The CateWiget class renders a template file Tpl/Cate/menu.html. The flexibility of calling the template is also the difference between using this method to implement Widget (in the Widget extension method, the renderFile method needs to be called to render the template).
Usage in menu.html template file: {$key}:{$title}
I hope this article will be helpful to everyone’s ThinkPHP framework programming.

http://www.bkjia.com/PHPjc/919615.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/919615.htmlTechArticleThinkPHP’s method of dynamically including files. This article describes the example of ThinkPHP’s method of dynamically including files. Share it with everyone for your reference. The specific analysis is as follows: Problem description: Working on...
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