ITeye博客

smarty 模板设计者

myfreespace · 2010-05-09 20:05 · smarty memcache · 编程语言 · HTML · PHP · Go · UP · Web · Markdown

<?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="me@domain.com" 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>




评论(1)

  1. #1 楼 · vb2005xu · 2010-06-02 10:29

    不知道你认为这样的模板看起来很清爽 还是原生代码舒服?

    
    <table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
    <?php web_control('hidden','menu[type]',array('value' => $menu['type']));?>
    <?php
    	$menu_options = array(
    		'menu[name]' => array(
    			'ctl'=>'textbox','text'=>'菜单名称',
    			'params'=>array('size' => 35,'value'=>(isset($menu['name'])?$menu['name']:null),'class'=>"formfield"),
    			'notice_caption' => ' (说明: 菜单的显示文本) *' ,
    			'error_caption' => ''
    		),
    		'menu[udi]' => array(
    			'ctl'=>'textbox','text'=>'UDI标识',
    			'params'=>array('size' => 35,'value'=>(isset($menu['udi'])?$menu['udi']:null),'class'=>"formfield"),
    			'notice_caption' => ' (格式: namespace::controller/action@module) *' ,
    			'error_caption' => ''
    		),
    		'menu[displayorder]' => array(
    			'ctl'=>'textbox','text'=>'显示次序',
    			'params'=>array('size' => 35,'value'=>(isset($menu['displayorder'])?$menu['displayorder']:0),'class'=>"formfield",'maxLength'=>2),
    			'notice_caption' => ' (格式: 小于16的整数,默认为0)' ,
    			'error_caption' => ''
    		),
    	);
    ?>
    
    <?php if (!empty($menu_options)): ?>
    
    <?php foreach ($menu_options as $option=>$values) :?>
    <tr class="tablecell">
       <td><?php echo htmlspecialchars("{$values['text']}: "); ?></td>	
       <td>
       <?php		
    		web_control($values['ctl'],$option,$values['params']);
       		web_control('label',null,array('caption'=>$values['notice_caption'],'class'=>'yes'));
       		web_control('label',"{$option}_error",array('caption'=>$values['error_caption'],'class'=>'no')); 
       	?>
       </td>
    </tr>	
    <?php endforeach?>
    
    <tr class="tablecell">
        <td colspan="2" align="center">
        <input type="submit" value="提交" class="formbutton">
        <input type="reset" value="重置" class="formbutton"></td>
    </tr>
    
    <?php endif; ?>
    </table>
    
    

评论功能维护中,当前仅展示历史评论。