Use tags in a template to query data

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange ideas and learn from new students.

/**
* General custom tags
* Usage:
* 1: Load
*
* 2: Call
*
* {$ V. id} -- {$ v. name}

*
* Note:
* 1. You can directly write SQL attributes on the tag.
* 2. Use the limit attribute to limit the number of queries.
* 3. Use the page attribute for data paging. Two parameters can be passed and separated by commas (,). The current page number of the previous parameter and the last parameter can be queried on each page.
* 4. Output {$ pageStr} on the view page}
* 5. Use method = "getOne (, 3)" to call the model. [directly call the method in quotation marks]
*/

Namespace Think \ Template \ TagLib;

Use Think \ Template \ TagLib;

Class Lx extends TagLib
{

Protected $ tags = array (
'LIST' => array ('attr' => 'table, order, limit, where, join, field, page, relation, method, cache, dump, SQL ', 'close' => 1 ),
);

Public function _ list ($ attr, $ content)
{
$ DefaultAttr = explode (',', $ this-> tags ['LIST'] ['attr ']);
$ Vars = array ();
Foreach ($ defaultAttr as $ v ){
If ($ attr [$ v]) {
$ Vars [$ v] = $ attr [$ v];
}
}
If (! Isset ($ vars ['table']) | empty ($ vars ['table']) {
Return false;
}
// Whether to use the specific method in the model or select
If (isset ($ vars ['method']) {
$ Select = '->'. $ vars ['method'];
} Else {
$ Select = "-> select ()";
}
// Limit and page
$ Limit = '';

If (! Isset ($ vars ['page']) {
If (isset ($ vars ['limit']) {
$ Limit = '-> limit ("'. $ vars ['limit']. '")';
}
} Else {
$ Limit = '-> page ("$ page, $ rows ")';
}
// Dump debugging
$ Dump = '';
If (isset ($ vars ['dump']) & $ vars ['dump'] = "true "){
$ Dump =' ';
}
// GetLastSql debugging
$ LastSql = '';
If (isset ($ vars ['SQL']) & $ vars ['SQL'] = "true "){
$ LastSql = 'echo"
". M ()-> getLastSql ();';
}
Unset ($ vars ['page'], $ vars ['limit'], $ vars ['method'], $ vars ['dump '], $ vars ['SQL']);
$ TmpStr = '';
$ Where = '';
$ Join = '';
Foreach ($ vars as $ k =>$ v ){
If ($ k = 'table '){
Continue;
}
If ($ k = 'where '){
$ Where = $ v;
}
If ($ k = 'join '){
$ Join = $ v;
}
$ TmpStr. = '->'. $ k. '("'. $ v .'")';
}
$ Str =' $ Page = I ("p", false )? I ("p"): I ("page ");
$ Page = empty ($ page )? 1: $ page;
$ Rows = I ("rows", 15 );
$ Table = '. ucfirst ($ vars ['table']).';
$ Data = D ("$ table") '. $ tmpStr. $ limit. $ select .';
Foreach ($ data as $ k = >$ v):?> ';
$ Str. = $ content;
$ Str. =' If (! Is_array ($ v )){
Break;
}
Endforeach;
'. $ LastSql .'
$ Total = M ("$ table")-> where ("'. $ where. '")-> join ("'. $ join. '")-> count ();
$ Page = new \ Think \ Page ($ total, $ rows );
$ PageStr = $ Page-> show ();
?> ';
$ Str. = $ dump;
Return $ str;
}


}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.