Use of widgets in a common ThinkPHP group-PHP development

Source: Internet
Author: User
In ThinkPHP, the use of the Widget tool is very simple and convenient to call. Let's take calling hotspot articles as an example to describe how to use widgets. Because the wblog program is a common group, I will use the Widget tool in the ThinkPHP common group. Download wblog3.1.3 because wblog is installed, what databases and data tables are available, so you don't have to bother creating them. Creating widgets-related directories and files is easy to use and convenient to call in ThinkPHP. Let's take calling hotspot articles as an example to describe how to use widgets. Because the wblog program is a common group, I will use the Widget tool in the ThinkPHP common group.


Download wblog3.1.3


As wblog is installed, what databases and data tables are available, so you don't have to create them.


Create widgets related directories and files

Create related Widget directories and files according to the following directory structure.

├ ── W3note normal group project directory

│ ─ ── Common

│ ─ ── Conf

│ Mongo── Lang

├ ── Demo-lib

│ Processing-Action

│ Behavior-Behavior

│ ├ ── Model

│ ─ ── Widget

│ ├ ── Tpl implements ── HotWidget. class. php

│ Sampled-Hot

│ ├─Hot.html


Code of the HotWidget. class. php file

 Field ('Id, title, hits ')-> order ('hits desc')-> limit (8)-> select (); return $ this-> renderFile ('', $ data) ;}}?>

Hot.html File Code

     
  
  •   {$h['title']|msubstr=0,30,'utf-8'}
  • Call the Widget Tool


    The call of the Widget tool is flexible and can be placed on the home page, list page, and content page. Next I will put it on the Content Page, open the W3note \ Tpl \ Read \ read.html file, and use {: W ('hot ')} to call the Widget tool we created above. The Code is as follows:

    Hot articles

      {: W ('hot ')}

    Final


    Summary: In general, when we call a hotspot article, we need to create a statement in the Controller and then write a circular Statement on the template. However, this method is cumbersome to call on multiple pages, and the use of the Widget tool can easily solve this problem.

    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.