Thinkphp methods for implementing dynamic include files, _php tutorial

Source: Internet
Author: User

Thinkphp implement a method for dynamically including files,


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:
Copy the Code code 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:
Copy the Code code as follows: Class Catewidget extends Action {
Public Function menu () {
Echo ' Menuwidget ';
}
}
4, the use of the template will need to change to:
Copy the code as follows: {~r (' cate/menu ', ' ', ' Widget ')}

5. If you need to use parameters when calling the widget, you can define this:
Copy the Code code as follows: Class Catewidget extends Action {
Public Function menu ($id, $name) {
echo $id. ': ' $name;
}
}
6. Parameters in the template call, using:
Copy the code as follows: {: R (' Cate/menu ', Array (5, ' thinkphp '), ' Widget ')}

The 5:thinkphp will be output.

7, to a more complex example:
Copy the Code code 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.

http://www.bkjia.com/PHPjc/919615.html www.bkjia.com true http://www.bkjia.com/PHPjc/919615.html techarticle thinkphp Implement the method of dynamic include file, this article describes the method of thinkphp implement dynamic include file. Share to everyone for your reference. The specific analysis is as follows: Problem description: doing sth .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.