<?php require_once '../config.php'; $ary = array('one','tow','three','go','now'); $contacts = array(array("phone" => "1", "fax" => "2", "cell" => "3"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")); $name = "Fred"; $change = 'chang myself from now'; $smarty->assign('test','test smarty'); $smarty->assign('ary',$ary); $smarty->assign('change',$change); $smarty->assign('contacts',$contacts); $smarty->assign('name',$name); $smarty->assign('cust_ids', array(1000,1001,1002,1003)); $smarty->assign('cust_names', array('Joe Schmoe','Jack Smith','Jane Johnson','Charlie Brown')); $smarty->assign('customer_id', 1001); $smarty->assign('data',array(1,2,3,4,5,6,7,8,9)); $smarty->assign('tr',array('bgcolor="yellow"','bgcolor="red"','bgcolor="green"')); $smarty->display('testnew.tpl'); ?>
{%$test|replace:"test":"quize"%}{%*替换replace:a:b 把a替换为b*%}
<br/> {%$test|capitalize%}{%*首字母大写*%} {%$test|truncate:5:"...":true%}{%*截取字符串 多余的用...代替,...也算在截取的数量中,*%} <br/> {%$test|count_words%}{%*计算字符的个数*%} <br/> {%$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"%}{%*返回时间字符串*%} {%*单数组循环 from 要循环的数组 item 要显示的变量 中间循环的要用 $item来循环*%} {%foreach from = $ary item = kk%} name:{%$kk%}<br/> {%/foreach%} {%*foreach 多多维数组循环*%} {%foreach name=outer item=contact from=$contacts%} {%foreach key=key item=item from=$contact%} {%$key%}: {%$item%}<br> {%/foreach%} {%/foreach%} {%*内建函数*%} {%capture name="haha"%}{%*把接受的值存储在变量haha中而不是显示在页面上,多用于加载模板可以通过*%} {%include file="header.tpl"%} {%$test%} {%/capture%} {%*$smarty.capture.haha*%} {%if $name eq "Fred"%} Welcome Sir. {%elseif $name eq "Wilma"%} Welcome Ma'am. {%else%} Welcome, whatever you are. {%/if%} {%*php%} echo "php mark"; {%/php*%} <br/> {%*自定义函数assgin为模板变量赋值,*%} {%counter start=0 skip=4 direction=down print=false%}{%*用于计数 start 为开始的位置 skip 为跳值,up down 为方向print为是否输出值*%} {%counter%}<br> {%counter%}<br> {%counter%}<br> {%counter%}<br> <table> {%foreach from = $ary item = kk%} <tr bgcolor="{%cycle values="#363636,#567342,red"%}"><td>{%$kk%}</td></tr> {%/foreach%} </table> {%*html_checkboses根据给定的数值组成复选框按钮,values复选框的值,check选择的 output显示的值*%} {%html_checkboxes name = id values=$cust_ids checked=$customer_id output=$cust_names separator="<br />"%} <select name=customer_id> {%*下拉菜单*%} {%html_options values=$cust_ids selected=$customer_id output=$cust_names%} </select><br/> {%*htm_radios 单选按钮*%} {%html_radios values=$cust_ids checked=$customer_id output=$cust_names separator="<br />"%}<br/> {%html_select_date%}{%*显示年月日*%}<br/> {%html_select_time%}<br/> {%html_table loop=$data cols=4 tr_attr=$tr table_attr='border="0"'%}<br/> {%mailto address="[email protected]" subject="Hello to you!"%}{%*对电子邮件进行编码*%}<br/> {%popup_init src="/javascripts/overlib.js"%} <A href="mypage.html" {%popup text="This link takes you to my page!"%}>mypage</A>