首页 > 代码库 > smarty笔记

smarty笔记

1. 文字拼接,文字查找,计数,截取

 1 <div class="upcoming_content"> 2     {assign var="comp" value=""} 3     {assign var="count" value="http://www.mamicode.com/0"} 4     {utilCall fn="utilGetCalendarTimeSlice" rn="calendar" cat_id="43" start_date=$start_date end_date=$end_date culture="en" limit="" sort_by="startDate"} 5     {if $calendar} 6         <ul class="upcoming_list"> 7         {foreach from=$calendar item=curr_item name=curr_item} 8             {foreach from=$curr_item item=event name=event} 9                 {assign var="c" value=http://www.mamicode.com/$event.id|cat:‘,‘}>10                 {if $comp|strpos:$c === false}  //文字查找11                     {assign var="comp" value=http://www.mamicode.com/$comp|cat:$event.id|cat:‘,‘}  //文字拼接12                     {assign var="count" value=http://www.mamicode.com/$count+1} //计数13                 {/if}14                 {if $count <= 10} //获取Top 1015                 <li class="event-{$event.id}">16                     <a href="/index.php?t=calendar.tpl&id={$event.id}&cat_id={$event.cat_id}&start_date={$event.start_date}&end_date={$event.end_date}" target="_blank">{$event.title|truncate:60:‘...‘}</a><br/>//文字截取17                     <span class="time02">{$event.start_date|date_format:"%B %e, %Y"}-{$event.end_date|date_format:"%B %e, %Y"}</span>18                 </li>19                 {/if}20             {/foreach}21         {/foreach}22         </ul>23     {/if}24 </div>

2. 分页

 1 <div id="pagelist" class="clearfix"> 2     <a href="/canadian-sailing-team-p191117&preview=1&pager=1">First</a> //第一页 3     {if $pagerInfo.currentPage > 1} //如果当前页大于1,则有pre链接 4         <a href="/canadian-sailing-team-p191117&preview=1&pager={$pagerInfo.previousPage}"><span>Pre</span></a> 5     {else} 6         <span>Pre</span> 7     {/if} 8     {section name=forpage loop=$pagerInfo.pageCount} //loop循环总数 9         {assign var="pindex" value=http://www.mamicode.com/$smarty.section.forpage.index+1} //index从0开始计数10         <a href="/canadian-sailing-team-p191117&preview=1&pager={$pindex}"><span {if $pagerInfo.currentPage == $pindex} class="cur" {/if}>{$pindex}</span></a>11     {/section}12  {if $pagerInfo.currentPage < $pagerInfo.pageCount}//如果当前页小于总页数,则有next链接13         <a href="/canadian-sailing-team-p191117&preview=1&pager={$pagerInfo.nextPage}"><span>Next</span></a>14     {else}15         <span>Next</span>16     {/if}17     <a href="/canadian-sailing-team-p191117&preview=1&pager={$pagerInfo.pageCount}">Last</a>18 </div>

3. 随机排序显示Page中的mediaObject

 1 {utilCall fn="utilGetPageById" rn="sliders" Id=‘156223‘} //获取page 2 <div class="img_ad"> 3     <div class="img_wrap"> 4         <img src="/files/SEMI_2011_logo_small.png" class="logo_wrap"> 5         {if $sliders->mediaObjects} 6         <ul id="jimgs"> 7             {assign var="img" value=""} 8             {assign var="title" value=""} 9             {foreach from=$sliders->mediaObjects item=slider} //获取所有mediaObjects10             {assign var="img" value=http://www.mamicode.com/$img|cat:$slider->getHumanReadbleURL(‘‘)|cat:‘,‘}>11             //把mediaObject的url地址用逗号分割放入img 字符串12             {assign var="title" value=http://www.mamicode.com/$title|cat:$slider->title|cat:‘,‘} 13             //把mediaObject的title用逗号分割放入title字符串14             {/foreach}15         </ul>16         {/if}17     </div>18 </div>19 {literal}20 <script>21 $(function () { 22     var title_str = "{/literal}{$title}{literal}";23     var img_str = "{/literal}{$img}{literal}";  //获取smarty参数24     if(img_str != undefined && img_str != "undefined"){25         randomSlider(img_str,title_str);  //随机排序26     }27     function randomSlider(img,title){ //随机排序函数28         var arr_img = img.split(,);29         var arr_title = title.split(,);30         var arr = new Array;31         var len = arr_title.length;32         arr_title.length = len - 1;33         arr_img.length = len - 1; //删除最后一个空值34         var len = len - 1;35         for(var i=0;i<len;i++){36             arr[i] = i;37         }38         var new_arr = arr.sort(randomSort);39         var _ul = $("#jimgs");40         for(var i=0;i<len;i++){41             var j = new_arr[i];42             var temp = <li><img src="http://www.mamicode.com/+arr_img[j]+" alt="+arr_title[j]+" /></li>;43             _ul.append(temp);44         }45         _ul.responsiveSlides({46             auto: true,47             pager: true,48             speed: 500,49             maxwidth: 94950         });51     }   52     function randomSort(a,b){53         return Math.random() - 0.5;54     }55 });56 </script>57 {/literal}