Instance analysis of Widget extension usages under thinkphp _php instance

Source: Internet
Author: User

The examples in this article describe the use of widget extensions under thinkphp. Share to everyone for your reference, specific as follows:

Widget extensions are used to output different content on a page as needed, and the widget extension is defined by defining the Widget class library under the project's Lib\widget directory, for example, by defining a widget to display the most recent comment:

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 exported, rather than direct output, using the returns.

Widgets can also invoke the RenderFile method of the Widget class, rendering the template for output.

Create 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;
  }


Create the corresponding test folder under this directory, and place the HTML page $this->rendfile the call below.

Index.html information by using loops to display data

<div>
  This is the widget called template page
  <foreach name= "id" item= "vo" > <span style= "color: #ff0000" >// Note: ID corresponds to $data key value </span>
    {$vo}
  </foreach>
  <br>
  <foreach name= "info" item= "Vo "> <span style=" color: #ff0000 ">//info corresponds to $data key value </span>
    <foreach name=" vo "item=" V ">
      {$v}
    </foreach>
  </foreach>
</div>

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 ();
  }


The index.html for the HTML page in the TPL that the action calls

The code is as follows:

<div>
This is the test called action<br>
{: W (' Test ', array ("ID" =>array ("id", "name", "title"), "info" = > $info))} <span style= "color: #cc0000" >//passed to TestWidget.class.php parameters to be passed in array format </span>
</ Div>

Run IndexAction.class.php

The page appears as follows:

That would include the contents of the show.html.

When does name use Widgte?

Widgets are usually some packaged JS effect components, directly call ' Add parameters can get some effect, such as tab menu, Carousel, Picture Carousel and other interactive effects

such as the Web site's menu bar. On the right often do not update the columns and the like ...

Easy to call, for example, on the left side of the page to display a news list, each page if you call this list, it is not necessary to write the same code in each controller, not tired, with the widget may only need to write once, and then the template multiple use

I hope this article will help you with the PHP program design based on thinkphp framework.

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.