好专业的beetl代码

原创
2013/08/17 01:29
阅读数 3.2K

无意中看到 @渔泯小镇  写的beetl翻页代码,这是一个典型的显示逻辑较为复杂的场景,很考验模板语言

写的非常专业,

/code/snippet_204433_21360

<%

002     var pageNumber = page.pageNumber;   // 当前页
003     var totalPage = page.totalPage;     // 总页数
004     var num_display_entries = 11;       // 每次显示多少页
005     var num_edge_entries = 2;           // 前后各多少页
006     var ellipse_text = "...";
007     // uri地址 如果不是/结束的 那么就表示带有参数,使用res风格
008     if (!strutil.endWith(uri,"/")) {
009         uri = uri + "-";
010     }
011      
012     if (page.totalRow > page.pageSize) {
013 %>
014 <div class="pagin clearfix">
015     <div class="pagin-head">
016 <%
017         // 上一页
018         if(pageNumber != 1) {
019             printf("<a href='%s%s'>&lt;</a>",uri,pageNumber-1);
020         } else {
021             printf("<span class='current prve'>&lt;</span>");
022         }
023         // 当前页
024         //printf(" <span class='current'>%s</span>",pageNumber);
025          
026         // 下一页
027         if(pageNumber != totalPage) {
028             printf(" <a href='%s%s'>&gt;</a>",uri,pageNumber+1);
029         } else {
030             printf(" <span class='current next'>&gt;</span>");
031         }
032 %>
033     </div>
034     <div class="pagin-body">
035     <%
036         // 总页数大于 (显示的页数+前后显示) 表示需要显示很多页
037         var pageCount = totalPage - (num_display_entries + num_edge_entries * 2);
038         if (pageCount > 0) {
039             var i = 1;
040             var len = 0;
041             var _start = trunc(num_display_entries / 2);
042             var _end = _start;
043             if (num_display_entries%2 == 0) {
044                 _start = _start - 1;
045             }
046              
047             // 页码-前
048             if (pageNumber - _start - num_edge_entries >= num_edge_entries) {
049                 while(i < num_edge_entries + 1) {
050                     printf("<a href='%s%s'>%s</a> ",uri,i,i);
051                     i = i + 1;
052                 }
053                 printf("<span>%s</span> ",ellipse_text);
054                  
055                 i = pageNumber-_start;
056                 len = pageNumber + _end+1;
057             } else {
058                 len = num_display_entries+1;
059             }
060              
061             var show_num_edge_entries_next = totalPage - pageNumber - _end - num_edge_entries > 0;
062             if (!show_num_edge_entries_next) {
063                 i = pageNumber - _start;
064                 if (totalPage - pageNumber - _end < 0) {
065                     i = totalPage - num_display_entries+1;
066                 }
067                 len = totalPage + 1;
068             }
069              
070             // 显示中间的那串页码
071             while(i < len) {
072                 if (pageNumber == i) {
073                     printf("<span class='current'>%s</span> ",i);
074                 } else {
075                     printf("<a href='%s%s'>%s</a> ",uri,i,i);
076                 }
077                 i = i + 1;
078             }
079              
080             // 页码-后
081             if (show_num_edge_entries_next) {
082                 printf("<span>%s</span> ",ellipse_text);
083                 i = totalPage + 1 - num_edge_entries;
084                 while(i < totalPage + 1) {
085                     printf("<a href='%s%s'>%s</a> ",uri,i,i);
086                     i = i + 1;
087                 }
088             }
089         } else {
090             // 显示全部页码
091             var i = 1;
092             while(i < totalPage + 1) {
093                 if (pageNumber == i) {
094                     printf("<span class='current'>%s</span> ",i);
095                 } else {
096                     printf("<a href='%s%s'>%s</a> ",uri,i,i);
097                 }
098                 i = i + 1;
099             }
100         }
101     %>
102     </div>
103 </div>
104 <%
105     }
106 %>
展开阅读全文
加载中
点击加入讨论🔥(5) 发布并加入讨论🔥
5 评论
21 收藏
3
分享
返回顶部
顶部