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