This article mainly introduces the method of realizing dynamic include files in ThinkPHP. It is a very practical skill in developing ThinkPHP projects. Friends in need can refer to it.
The example of this article describes the implementation of dynamic include files in ThinkPHP. method. 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 doing projects. Generally, the homepage contains headers and steps. In order to facilitate management, these need to be implemented using include files. ThinkPHP Provides a method to include files. The above is the simplest way to include operations. However, during the running process, I found that only the template file is requested when requesting, which is the so-called static inclusion. But if you encounter a menu It is difficult to handle if it is dynamically generated.
Find a solution on the Internet: use Widget
1. We implement a Widget for classified display on the page. First, we have to implement what we first Define a Widget controller layer CateWidget, 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 use the R method in the template Call this Widget (the extended Widget method uses the W method in the template).
{:R('Cate/Menu','','Widget')}
The output result 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 usage in the template needs to be changed to:
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 :
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:
The code is as follows:
{:R('Cate/Menu',array(5,'thinkphp'),'Widget')}
then Will output 5: thinkphp
7. Let’s take a more complicated 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. The CateWiget class renders a template file Tpl/ Cate/menu.html, the flexibility of calling templates is also the difference between using this method to implement Widgets (in Widget extension mode, you need to call the renderFile method to render the template).
Usage in the menu.html template file: {$key}:{$title}
The above is the entire content of this article. I hope it will be helpful to everyone’s study. More related Please pay attention to the PHP Chinese website for content!
Related recommendations:
About the code of ThinkPHP file cache class
Analysis of the functions and usage of widgets in thinkPHP5 framework
The above is the detailed content of How to dynamically include files using ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!