smarty、smarty格式化、smarty整數、smarty float、smarty各種轉換方式、smarty日期轉換等等 (轉)...

weixin_34054866發表於2015-12-01
<?
require("setup.php");
define('PAGETITLE','pagtitle');
function insert_top($lid,$sid)
{
echo "insert function";
}
$smarty=new SmartyRebuild();
$smarty->assign('name','Linux');
$smarty->assign('title','線上有有要有和藹是是非非要要');
$smarty->assign('english_word','my name x4 is linux');
$smarty->assign('money','344.2223');
$smarty->assign('member',array('fax'=>'010-95888484','email'=>'linux@admin.com',
     'phone'=>array('home'=>'8488484','mobil'=>'5325325325353')));
$tempMember=array();
$tempMember['member']='dsgads';
$tempMember["phone"]["home"]="444333";
$contacts=array();
$contacts[]["fax"]="435454";
$contacts[]["phone"]="223523523";
$contacts[]["fax"]="42532532";
$contacts[]["phone"]="7888888";
$contacts2=array(array('fax'=>'32523','phone'=>'325132532'),array('fax'=>'2313532','phone'=>'325325132523'));
$contacts3=array();
$contacts3[0]="23523";
$contacts4=array();
$contacts4[0][0]="23523";
$contacts4[0][1]="23523";
$contacts3[1]="23523";
$contacts4[1][0]="23523";
$contacts4[1][1]="23523";
$contacts3[2]="23523";
$contacts4[2][0]="23523";
$contacts4[2][1]="23523";
$contacts3[3]="23523";
$contacts4[3][0]="23523";
$contacts4[3][1]="23523";
$smarty->assign('contacts',$contacts);
$smarty->assign('contacts2',$contacts2);
$smarty->assign('contacts3',$contacts3);
$smarty->assign('contacts4',$contacts4);
$smarty->assign('tempmember',$tempMember);
$smarty->display('index.tpl');
?>



smarty code:

     

{config_load file='config.conf'}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>Smarty學習</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
</HEAD>

<BODY>
{capture name=banner}{*註釋掉中間顯示內容*}
{include file="banner.tpl"}
{/capture}
{include file="banner.tpl" title="Smarty學習"}
{$smarty.get.page}<br/>
{$smarty.post.page}<br/>
{$smarty.cookies.name}<br/>
{$smarty.server.SERVER_NAME}<br/>
{$smarty.env.PATH}<br/>
{$smarty.session.id}<br/>
{$smarty.request.username}<br/>
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}<br/>
{$smarty.const.PAGETITLE}{*常量*}<br/>
Hello {$name}{*變數*}<br/>
{html_select_date display_days=yes}<br/>
{$member.fax}<br/>
{$member.phone.home}<br/>
{$tempmember.member}<br/>
{$tempmember.phone.home}{*多維陣列*}<br/>
{#error_message#}{*配置檔案屬性*}<br/>
{$name|lower}{*smarty字串函式*}<br/>
{$name|upper}{*smarty字串函式*}<br/>
{'now'|date_format:'%Y-%m-%d'}{*smarty日期函式*}<br/>
{$title|truncate:11:"..."}{*截斷字串超長加...*}<br/>
{mailto|lower address="mailto:linux@admin.com%22%7D%7B*mailto郵件*}<br/>
mailto:%7B$tempmember%7C@count}{*資料的總和*}<br/>
{$english_word|capitalize}{*首字母大寫*}<br/>
{$english_word|capitalize:true}{*每個單詞的首字母大寫*}<br/>
{$title|count_characters:true}{*字串的長度加true字元空格,不加true不計算空格*}<br/>
{$title|cat:'字串後加入字元'}{*拼接字串*}<br/>
{$title|count_words}{*計算字元數量*}<br/>
{$title2|default:"dsgadsgds"};{*如果變數不存在就輸出預設值*}<br/>
{$english_word|escape:html}{*將字串中的HTML轉換為HTML編碼方式*}<br/>
{$english_word|escape:htmlall}{*將字串中的HTML轉換為HTML編碼方式*}<br/>
{$english_word|escape:url}{*將字串中的HTML轉換為URL編碼方式*}<br/>
{$english_word|escape:quotes}{*將字串中的HTML轉換為HTML編碼方式*}<br/>
{$title|indent:100}{*縮排指定寬度*}<br/>
{$title|regex_replace:"/要/":"愛"}{*正規表示式替換字元*}<br/>
{$title|replace:"要":"愛"}{*Replace替換字元*}<br/>
{$english_word|spacify:"||"}{*分隔字串*}<br/>
{$money|string_format:"%.2f"}{*格式化數字為Float數*}<br/>
{$money|string_format:"%d"}{*格式化數字為INT數*}<br/>
{$english_word|strip:" "}{*替換空格*}<br/>
{$english_word|strip_tags:false}{*替換標籤*}<br/>
{$english_word|truncate:10:"..."}{*擷取指定長度字元,預設擷取80字元長度*}<br/>
{$english_word|wordwrap:10}{*多長後字串換行,預設換行長度為80*}<br/>
{$english_word|upper|spacify|truncate:30:"..."}{*Smarty支援多個函式的組合,此組合為字串先大寫然後分隔然後擷取指定長度*}<br/>
{foreach name=outer item=contact from=$contacts}{*foreach迴圈陣列*}
  {foreach key=key item=item from=$contact}
  {$key}:{$item}<br/>
  {/foreach}
{/foreach}
{foreach name=outer item=contact2 from=$contacts2}
  {foreach key=key item=item from=$contact2}
  {$key}:{$item}<br/>
  {/foreach}
{/foreach}
{include_php file="function.php"}{*包含指定的PHP檔案*}
{insert name="top" lid="toplid" sid="topsid"}{*呼叫insert_top PHP函式,函式引數為lid和sid的值*}
{if 1 eq 1}{*條件判斷語句*}
  1=1
{elseif 1 ne 1}
  1!=1
{elseif 1 gt 1}
  1>1
  {elseif 1 lt 1}
  1<1
  {elseif 1 gte 1}
  1>=1
  {elseif 1 lte 1}
  1<=1
  {elseif not 1 eq 1}
  not 1=1
  {elseif (1 mod 1) eq 1}
  <font color="red">1%1=1</font>
  {else}
  你的人品真不好
  {/if}
{ldelim}function insert_top {rdelim} is insert_top {*輸出中括號*}
{literal}{*加入一段HTML,指令碼程式碼等*}
<script language="javascript" type="text/css">
<!--
function load()
{
  alert('頁面載入....');
}
//-->
</script>
  {/literal}
  {*包含一段PHP語句*}
  {php}
  echo "php程式碼執行";
  {/php}
  {section name=member loop=$contacts3 show=true}{*while迴圈 show是否SHOW section中的迴圈內容*}
   {if $smarty.section.member.first}
  first
{/if}
{if $smarty.section.member.last}
  last
{/if}
current loop rownum:{$smarty.section.member.rownum}<br/>
   current loop iteration:{$smarty.section.member.iteration}<br/>
   index:{$smarty.section.member.index} index_prev:{$contacts3[member.index_prev]} index_next:{$contacts3[member.index_next]} index:{$contacts3[member.index]} member:{$contacts3[member]}
   {section name=memberchild loop=$contacts4[member] max=2}
    memberchild:{$contacts4[member][memberchild]}
   {/section}
   total:{$smarty.section.member.total}{*資料長度或總記錄數*}
  {/section}

</BODY>
</HTML>

相關文章