日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

SMARTY核心

發(fā)布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SMARTY核心 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://www.smarty.net/
http://smarty.php.net/manual/en/


1.配置

??????? define("ROOTPATH",dirname(__FILE__)."/../");
??????? require_on ce("smarty/Smarty.class.php");

??????? /**
???????? * Smarty Template Class Initializtion
???????? */
??????? if( @constant( "ENABLED_TPL" ) ) {
??????????? $obj_smarty = new Smarty();
??????????? $obj_smarty->compile_check = true; //Open Compile Check
??????????? $obj_smarty->debugging = false;// false || DEBUG || DEBUG_TPL; //debug On-off
??????????? $obj_smarty->template_dir = ROOT_PATH."html"; //Set Template Document Root
??????????? $obj_smarty->compile_dir = ROOT_PATH."cache"; //Set Template Cache File Document Root
??????????? $obj_smarty->left_delimiter = "<!!"; //Set Template Left Delimiter
??????????? $obj_smarty->right_delimiter = "!!>"; //Set Template Right Delimiter
??????????? /**
???????????? * 0:Smarty默認值,表示不對模板進行緩存?
???????????? * 1:表示Smarty將使用當前定義的cache_lifetime來決定是否結(jié)束cache
???????????? * 2:表示Smarty將使用在cache被建立時使用cache_lifetime這個值
???????????? * 習慣上使用true與false來表示是否進行緩存
???????????? */
??????????? $obj_smarty->caching = true;
??????????? $obj_smarty->cache_lifetime = 60 * 60 * 24;//-1:緩存從不過期,0:每次執(zhí)行緩存總是被重新建立?
??????? }?


2.循環(huán),section,sectionelse

??????? <!!sectionelse!!> 用來表示傳入的數(shù)組變量為空時對模板進行的處理。

??????? <!!section name=on e loop=$arr_result start=5 step=2 max=3 show=$show_customer_info!!>?
?????????? <!!$arr_result[on e].name!!>?
?????????? <!!$smarty.section.on e.iteration!!> //display the current loop iteration
?????????? <!!$smarty.section.on e.index!!>???? //display the current loop index, starting with zero?
?????????? <!!$smarty.section.on e.first!!>???? //first is set to true if the current section iteration is the first on e
?????????? <!!$smarty.section.on e.last!!>????? //last is set to true if the current section iteration is the last on e.?
?????????? <!!$smarty.section.on e.loop!!>????? //display the last index number that this section looped
?????????? <!!$smarty.section.on e.show!!>????? //show is a boolean value, true or false
?????????? <!!$smarty.section.on e.total!!>???? //display the number of iterations that this section will loop?? ???
?????????? <!!$smarty.section.name.rownum+$offset!!>//display the current loop iteration, starting with on e $offset為基值
??????? <!!/section!!>


??????? <!!section name=on e loop=$arr_result!!>
??????????? <input type="checkbox" name="<!!$name!!>[]" id="<!!$name!!>_<!!$smarty.section.on e.index+1!!>" value="<!!$arr_result[on e].value!!>"?
??????????? <!!if ($arr_result[on e].value|inarray:$arr_values) !!>checked<!!/if!!>/><!!$arr_result[on e].value!!>
??????? <!!/section!!>?

??????? <!!section name=on e loop=$arr_result!!>
??????????? <option value="<!!$arr_result[opt].value!!>" <!!if $arr_result[on e].value==$selectedvalue !!>selected<!!/if!!>><!!$arr_result[on e].option!!></option>
??????? <!!/section!!>


??????? 表格排版 1行2列?
??????????????? <!!if $smarty.section.on e.index%2 eq 0 !!>
??????????????? <!!if $smarty.section.on e.index%2 eq 1 !!>

??????? 奇偶數(shù)用于斑馬線效果
??????????????? <!!if $smarty.section.on e.rownum%2 eq '0'!!>...<!!else!!>...<!!/if!!>

??????? 生成斑馬線
??????????????? <div style="background:<!!cycle values='#F0F8FF,#F0FFFF,#FFFAF0,#F0FFF0'!!>" id="div_msg_<!!$smarty.section.on e.rownum!!>">
??????????????? ...
??????????????? </div>


3.表格,html_table table_foreach

??????? <!!html_table cols=4 table_attr=' width="95%" border="0" cellspacing="0"?
?????????? cellpadding="2" ' tr_attr=' align="left" valign="top" ' td_attr=' width="25%" ' loop=$arr_result!!>
??????????
??????? <!!table_foreach from=$addproducts item=sam key=id cols=4 table_attr='width="100%" border="0" cellspacing="4"
??????? cellpadding="0"' tr_attr='' td_attr=' width="25%"align="center" valign=""'!!>
??????????
??????? <a href="<!!$sam.adurl!!>">
????????? <img src="upload/images/<!!$sam.adpic!!>" alt="<!!$sam.adalt!!>"?
??????? width="100" height="100" border="0" class='picline'></a>
??????? <a href="<!!$sam.adurl!!>" title="<!!$sam.adalt!!>"><!!$sam.adtitle|substrgb:'0':'20':''!!></a>?

??????? <!!/table_foreach!!>?


4.包含文件,include ????

??????? <!!include file="header.tpl"!!>


5.不解析,literal ????

??????? <!!literal!!> 這里是js或css,模板不解析 <!!/literal!!>


6.條件判斷,if,elseif,else ????

??????? <!!if $name eq "Fred" and ($type eq '1' || $type eq '2')!!>
??????????? Welcome Sir.
??????? <!!elseif $name eq "Wilma"!!>
??????????? Welcome Ma'am.
??????? <!!else!!>
??????????? Welcome, whatever you are.
??????? <!!/if!!>

??????? "eq", "ne","neq", "gt", "lt", "lte", "le", "gte" "ge","is even","is odd",
??????? "is not even","is not odd","not","mod","div by","even by","odd by","==","!=",">", "<","<=",">="?


7:字符替換,replace ????

??????? |replace:"1":"2"????? 1--將被替換的字符串 2--用來替換的文本
??????????????? <!!$articleTitle|replace:"pear":"apple"!!>


8:去除Html標簽,strip_tags ???

??????? 去除Html標簽 去除在<和>之間的所有標簽,包括<和>.
??????????????? <!!$articleTitle|strip_tags!!>


8:字符截取,truncate,substrgb,mbstrutf8 ???

??????? 截取|truncate:"1":"2":"3"?? 1--指定截取多少字符 2--截取后加在截取詞后的字符串?? 3--檢查是否截取到詞的邊界 Default(false)
??????????????? <!!$articleTitle|truncate:30:"...":true!!>
??????????????? <!!$articleTitle|substrgb:"0":"23":".."!!>
??????????????? <!!$arr_story[$top18].rm_title|mbstrutf8:"0":"34":"..."!!>

??????????????? <!!$arr_result[on e].SP_CONTENTS|substrgb:"0":"16":"..."!!> 可以解決中文截取亂碼問題


9:注釋,Comments ???

??????? 注釋
??????????????? <!!* this is a comment *!!>?


10:日期格式,date_format ???

??????? 日期格式
??????????????? <!!$smarty.now|date_format!!>
??????????????? <!!$smarty.now|date_format:"%Y/%m/%d"!!>
??????????????? <!!$smarty.now|date_format:"%Y-%m-%d"!!>
??????????????????????? %y:顯示兩位數(shù)字年份, 00 至 99
??????????????????????? %Y:顯示四位數(shù)字年份
??????????????????????? %m:顯示數(shù)字月份,01-31
??????????????????????? %b:顯示英文月份,簡稱
??????????????????????? %B:顯示英文月份,全稱
??????????????????????? %d 顯示數(shù)字日份,01-31
??????????????????????? %D 顯示英文日份,簡稱3 個字母 Mon 到 Sun?


11:日期下拉列表,html_select_date ???

??????? 日期下拉列表
??????????????? <!!html_select_date prefix="StartDate" start_year="-5" end_year="+5" field_order="YMD" month_format="%m" display_days=false time="$time"!!>
??????????????? time="2006-06-06" 可以給定輸出想要的值


12:循環(huán),foreach,foreachelse

??????? 選擇性的section循環(huán)
??????????????? from 需要循環(huán)的數(shù)組的名稱?
??????????????? item 當前元素的變量名?
??????????????? key?? 當前關鍵字的變量名
??????????????? name 訪問foreache屬性的foreach循環(huán)名

??????????????? $smarty->assign("arr_result", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
??????????????????????? array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));

??????????????? <!!foreach name=outer item=contact from=$arr_result!!>
??????????????????? <!!foreach key=key item=item from=$contact!!>
???????????????????????? <!!$key!!>: <!!$item!!>
??????????????????? <!!/foreach!!>
??????????????? <!!/foreach!!>

??????????????? <!!foreachelse!!> 在from變量沒有值的時候被執(zhí)行


13:下拉列表,html_options ???

??????? 輸出下拉列表
??????????????? name???????? name of select group
??????????????? values?????? an array of values for dropdown
??????????????? output?????? an array of output for dropdown
??????????????? selected???? the selected option element(s)
??????????????? options????? an associative array of values and output
??????????????????????? <select name=customer_id>
??????????????????????????? <!!html_options values=$cust_ids selected=$customer_id options=$cust_options output=$cust_names!!>
??????????????????????? </select>


14:換行替換,nl2br ???

??????? 換行符替換成<br />
??????????????? <!!$articleTitle|nl2br!!>


15:默認值,default ???

??????? 當變量為空或者未分配的時候,將由給定的默認值替代輸出.
??????????????? <!!$articleTitle|default:"no title"!!>


16:字符處理,capitalize,ucwords ???

??????? 將變量里的所有單詞首字大寫
??????????????? <!!$articleTitle|capitalize!!>
??????????????? <!!$articleTitle|ucwords!!>


17:字符串連接,cat ???

??????? 字符串連接 將cat里的值連接到給定的變量后面.
??????????????? <!!$articleTitle|cat:" yesterday."!!>


18:字符處理,strtolower ???

??????? 將變量里的所有單詞小寫.
??????????????? <!!$articleTitle|strtolower!!>


19:獲取字符串,fetch

??????? 獲取字符串
??????????????? $str_html = $obj_smarty->fetch("index.html");


20:capture

??????? capture函數(shù)的作用是收集模板輸出的數(shù)據(jù)到一個變量里,而不是把它們輸出到頁面.
??????? 任何在 {capture name="foo"}和{/capture}之間的數(shù)據(jù)都被收到了由函數(shù)的名稱屬性指定的變量里($foo).
??????? 收集的信息可以用在特殊變量$smarty里.
??????? 例如capture.foo就收集了以上數(shù)據(jù).如果函數(shù)沒有名字屬性,將使用"default".
??????? 每個{capture}都必須對應{/capture},也不能嵌套使用capture函數(shù)
??????? {capture name=banner}
??????????? 1
??????? {/capture}
??????? {capture name=banner2}
??????????? 2
??????? {/capture}

??????? {if $smarty.capture.banner ne ""}
??????????? <tr>
??????????????? <td>
??????????????????? {$smarty.capture.banner}
??????????????? </td>
??????????????? <td>
??????????????????? {$smarty.capture.banner2}
??????????????? </td>
??????????? </tr>
??????? {/if}
??????? 抓取數(shù)據(jù)存放在變量中 可以在一個頁面中重復使用


21:include_php,php

??????? include_php
??????????????? 在模板里加載php文件
??????? php
??????????????? 可以直接使用php代碼 相當于<?php???? ?>
??????? <!!include_php file="phpinfo.php"!!>
??????? <!!php!!>
??????????? include("phpinfo.php");
??????????? echo date("h:i:s");
??????? <!!/php!!>


22:insert

??????? 使模板的一部分不被緩存
??????? //demo.php
??????????????? $int_cache_id = sprintf('%X', crc32("index.html"));
??????????????? if( !$obj_smarty->is_cached('index.html', $int_cache_id) ) {
??????????????????? $obj_smarty->assign("str_date",date("h:i:s"));
??????????????? }
??????????????? $obj_smarty->display("index.html",$int_cache_id);

??????????????? function insert_getBanner( $arrParams ) {
??????????????????? return $arrParams["lid"]."-andy-".$arrParams["sid"];
??????????????? }

??????? //index.html
??????????????? <!!$str_date!!>
??????????????? <!!insert name="getBanner" lid="1" sid="2" !!>


23:ldelim,rdelim

??????? ldelim and rdelim are used for displaying the literal delimiter, in our case "{" or "}".?
??????? 輸出變量 不做解析?
??????????????? <!!ldelim!!>funcname<!!rdelim!!> is how functions look in Smarty!
??????? OUTPUT:
??????????????? <!!funcname!!> is how functions look in Smarty!


24:strip

??????? 去除<!!strip}<!!/strip!!>間的所有空格


25:{$smarty}保留變量

??????? 頁面請求變量
??????? {* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *}
??????? {$smarty.get.page}

??????? {* display the variable "page" from a form a form (POST) *}
??????? {$smarty.post.page}

??????? {* display the value of the cookie "username" *}
??????? {$smarty.cookies.username}

??????? {* display the server variable "SERVER_NAME" *}
??????? {$smarty.server.SERVER_NAME}

??????? {* display the system environment variable "PATH" *}
??????? {$smarty.env.PATH}

??????? {* display the php session variable "id" *}
??????? {$smarty.session.id}

??????? {* display the variable "username" from merged get/post/cookies/server/env *}
??????? {$smarty.request.username}

??????? {$smarty.now}變量用于訪問 當前時間戳.

??????? 可以用 date_format調(diào)節(jié)器格式化輸出.
??????? {* use the date_format modifier to show current date and time *}
??????? {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

??????? {$smarty.const}
??????? 可以直接訪問PHP常量.
??????? {$smarty.const._MY_CONST_VAL}

??????? {$smarty.template}
??????? 當前被處理的模板的名字


26:組合使用調(diào)節(jié)器 |
?? ????
??????? <!!$articleTitle!!>
??????? <!!$articleTitle|upper|spacify!!>
??????? <!!$articleTitle|lower|spacify|truncate!!>
??????? <!!$articleTitle|lower|truncate:30|spacify!!>
??????? <!!$articleTitle|lower|spacify|truncate:30:". . ."!!>


27:fetch

??????? 抓取數(shù)據(jù)
??????????????? {* include some javas cript in your template *}
??????????????? {fetch file="/export/httpd/www.domain.com/docs/navbar.js"}

??????????????? {* embed some weather text in your template from another web site *}
??????????????? {fetch file="http://www.myweather.com/68502/"}

??????????????? {* fetch a news headline file via ftp *}
??????????????? {fetch file="ftp://user:password@ftp.domain.com/path/to/currentheadlines.txt"}

??????????????? {* assign the fetched contents to a template variable *}
??????????????? {fetch file="http://www.myweather.com/68502/" assign="weather"}
??????????????? {if $weather ne ""}
??????????????????? {$weather}
??????????????? {/if}

總結(jié)

以上是生活随笔為你收集整理的SMARTY核心的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。