The examples in this paper describe how thinkphp implements dynamic include files. Share to everyone for your reference. The specific analysis is as follows:
Problem Description: In the project to encounter problems, is also a very common problem, the General home page contains the head and footsteps, in order to facilitate management, these need to use the include file to implement, Thinkphp provides a way to include files, the above is the simplest way to include the operation, but in the process of running I found that The request is simply a template file, which is called a static inclusion, but it is difficult to see if the menu is dynamically generated.
Find a workaround online: using widgets
1, we implement a category display widget in the page, first we want to implement we first define a widget controller layer catewidget, as follows:
The code is as follows:
Class Catewidget extends Action {
Public Function menu () {
Return ' Menuwidget ';
}
}
Note that it is defined in the widget package, and there is a difference between the general action and the
2. Then, we call this widget through the R method in the template (the extension widget uses the W method in the template), if the R function does not understand the reference 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:
The code is as follows:
Class Catewidget extends Action {
Public Function menu () {
Echo ' Menuwidget ';
}
}
4, the use of the template will need to change to:
The code is as follows:
{~r (' cate/menu ', ' ', ' Widget ')}
5. If you need to use parameters when calling the widget, you can define this:
The code is as follows:
Class Catewidget extends Action {
Public Function menu ($id, $name) {
echo $id. ': ' $name;
}
}
6. Parameters in the template call, using:
The code is as follows:
{: R (' Cate/menu ', Array (5, ' thinkphp '), ' Widget ')}
The 5:thinkphp will be output.
7, to a more complex example:
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, Catewiget class rendered a template file tpl/cate/menu.html, call template flexibility is also used in this way to implement the difference widget (widget extension method needs to call the RenderFile method render template).
Usage in menu.html template file: {$key}:{$title}
It is hoped that this article will be helpful to everyone's thinkphp framework design.