Example analysis of widget extension usages under thinkphp,
This example describes the use of widget extensions under thinkphp. Share to everyone for your reference, as follows:
The widget extension is used to output different content on the page as needed, and the widget extension is defined as the Widget class library defined under the project's Lib\widget directory, for example, a widget that shows the most recent comment is defined below:
Located in lib\widget\showcommentwidget.class.php
The Widget class library needs to inherit the widget class and must define the Render method implementation, for example:
The Render method must return the string information to be output using return instead of the direct output.
Widgets can also invoke the RenderFile method of the Widget class, rendering the template and outputting it.
Build the widget directory in the project's Lib directory, with the action directory sibling.
Establish 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; }}
Set up the corresponding test folder under this directory, and put the HTML page of the $this->rendfile call below.
index.html information for displaying data by using loops
This is the template page for the widget call
//NOTE: The ID corresponds to the key value of the $data {$VO}
//info The key value
corresponding to the $data
{$v}
Action method:
IndexAction.class.php
The code is as follows:
Class Testaction extends action{public function Index () { $info =array (Array ("1", "AA", "title"), Array ("2", " BB "," Title2 ")); $this->assign ("info", $info); $this->display (); }}
Action is called by the index.html of the HTML page in the TPL
The code is as follows:
This is the action of the Test call.
Arguments passed to TestWidget.class.php, to be passed in array format
Run IndexAction.class.php
The page appears as follows:
So we're going to include the contents of the show.html.
When does name use Widgte?
Widgets are usually packaged JS effect components, direct call ' Add parameter can get some effect, for example, tab menu, Carousel, Picture Carousel and other interactive effects
such as the site's menu bar. On the right is often not updated columns and the like ...
Convenient multiple calls, such as on the left side of the page to display a news list, each page if you call this list, is it necessary to write the same code in each controller, not tired, with the widget may only need to write once, and then use the template multiple times
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.
Articles you may be interested in:
- thinkphp instance of widget extension
- New usage of ThinkPHP3.1 widgets
- thinkphp Framework Design and extension
- thinkphp Behavior Extension Behavior Application example detailed
- thinkphp Application Pattern Extension
http://www.bkjia.com/PHPjc/1084516.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084516.html techarticle The example of widget extension usage under thinkphp is described in this paper, which describes the usage of widget extension under thinkphp. Share to everyone for your reference, as follows: Widget extension for the page root ...