search
Homephp教程php手册ThinkPHP实现动态包含文件的方法,

ThinkPHP实现动态包含文件的方法,

本文实例讲述了ThinkPHP实现动态包含文件的方法。分享给大家供大家参考。具体分析如下:

问题描述:在做项目的时候遇到问题,也是很常见的问题,一般主页包含头部和脚步,为了方便管理,这些都需要使用包含文件来实现,ThinkPHP提供了包含文件的方法,以上是最简单的包含的操作的方式,但是在运行的过程中我发现,在请求的时候只是请求的是模板文件,也就是所谓的静态包含,但是如果遇到菜单是动态生成的就很难办了.

在网上找到一个解决办法:使用Widget

1、我们在页面中实现一个分类显示的Widget,首先我们要实现我们首先定义一个Widget控制器层 CateWidget,如下:

复制代码 代码如下:

class CateWidget extends Action { 
    public function menu(){ 
        return 'menuWidget'; 
    } 
}


注意是在Widget包中定义,和一般的Action还是有区别的

2、然后,我们在模版中通过R方法调用这个Widget(扩展Widget方式在模版中使用的是W方法),如果对R函数不了解的参考这里.(http://www.thinkphp.cn/info/134.html)

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

执行后的输出结果是:menuWidget

3、如果,CateWidget类的menu方法改成:

复制代码 代码如下:

class CateWidget extends Action { 
    public function menu(){ 
        echo 'menuWidget'; 
    } 
}


4、模版中的用法则需要改成:

复制代码 代码如下:

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

5、如果需要在调用Widget的时候 使用参数,可以这样定义:

复制代码 代码如下:

class CateWidget extends Action { 
    public function menu($id,$name){ 
        echo $id.':'.$name; 
    } 
}


6、模版中的参数调用,使用:

复制代码 代码如下:

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

则会输出 5:thinkphp

7、来一个复杂一点的例子:

复制代码 代码如下:

class CateWidget extends Action { 
    public function menu(){ 
        $menu = M('Cate')->getField('id,title'); 
        $this->assign('menu',$menu); 
        $this->display('Cate:menu'); 
    } 
}


8、CateWiget类渲染了一个模版文件Tpl/Cate/menu.html,调用模版的灵活也是使用这种方式实现Widget的区别(Widget扩展方式中需要调用renderFile方法渲染模版)。

在menu.html模版文件中的用法:{$key}:{$title}

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment