Home Backend Development PHP Tutorial thinkPHP下的widget扩展用法实例分析_PHP

thinkPHP下的widget扩展用法实例分析_PHP

May 28, 2016 am 11:49 AM
thinkphp Expand

本文实例讲述了thinkPHP下的widget扩展用法。分享给大家供大家参考,具体如下:

Widget扩展用于在页面根据需要输出不同的内容,Widget扩展的定义是在项目的Lib\Widget目录下面定义Widget类库,例如下面定义了一个用于显示最近的评论的Widget:

位于Lib\Widget\ShowCommentWidget.class.php

Widget类库需要继承Widget类,并且必须定义render方法实现,例如:

render方法必须使用return返回要输出的字符串信息,而不是直接输出。

Widget也可以调用Widget类的renderFile方法,渲染模板后进行输出。

在项目的lib目录下建立widget目录,与action目录同级。

建立TestWidget.class.php:

class TestWidget extends Widget{
  public function render($data){
    //print_r($data);
    $data['id']=$data['id'];
    $data['info']=$data['info'];
    $content = $this->renderFile('index',$data);
    //print_r($content);
    return $content;
  }
}

Copy after login

在此目录下建立对应的Test文件夹,下面放$this->rendFile调用的html页面。

index.html 通过使用循环显示数据的信息

<div>
  这是widget调用的模板页面
  <foreach name="id" item="vo"> <span style="color:#ff0000">//注意:id对应与$data的键值</span>
    {$vo}
  </foreach>
  <br>
  <foreach name="info" item="vo"> <span style="color:#ff0000">//info对应于$data的键值</span>
    <foreach name="vo" item="v">
      {$v}
    </foreach>
  </foreach>
</div>
Copy after login

action方法:

IndexAction.class.php

代码如下:

class TestAction extends Action{
  public function index(){
    $info=array(array("1","AA","title"),array("2","BB","title2"));
    $this->assign("info",$info);
    $this->display();
  }
}

Copy after login

action调用的tpl中的html页面的为index.html

代码如下:

<div>
这个是test调用的action<br>
{:W('Test',array("id"=>array("ID","name","title"),"info"=>$info))} <span style="color:#cc0000">//传递给TestWidget.class.php 的参数,要以array格式传递</span>
</div>

Copy after login

运行IndexAction.class.php

页面显示如下:

这样就把show.html中的内容包含进来了

name什么时候用widgte呢?

widget通常是一些封装好了的JS效果组件,直接调用‘加入参数就可以得到一些效果,例如,TAB菜单、旋转木马、图片轮播等交互效果 

比如网站的菜单栏。右边常不更新的栏目之类的…

方便多次调用,比如在页面上左侧显示一个新闻列表,每个页面如果都调用这个列表,是不是得在每个控制器里写一遍相同的代码,不累么,用widget可能只需要写一次,然后模板中多次使用

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

Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles