Smarty笔记

  smarty是个很不错的模板引擎,通过它你可以实现代码与页面分离,恩,就是这样,说起模板引擎,听起来挺玄乎的,其实很简单,就是把html中特定符号(smarty中为{})中的内容做解析,与其它模板引擎作用相似,如Twig等。

  smarty使用起来也比较方便,只需要php文件与模板文件(我喜欢用html的格式,所见即所得,smarty是tpl,twig是.twig,都已经脱离了html,为什么搞得这么麻烦呢,能有人告诉我么?)一一对应就可以了。

  一个好的模板引擎,不仅能展示变量的值,还需要满足一些基本的其它功能,如实现循环加载,条件加载等,还能实现一些特殊的html元素,如select,radio button,checkbox等的处理,而smarty都很好的完成了这些功能,我把我的笔记代码,放在这里,只做记忆。参考文件:大师兄教程,这个教程好老,2004年的,但愈久弥新。

 现看目录结构:

主要需修改的为index.php(smarty中的数据文件)与templates目录下的index.html文件

先看index.html中的WYSIWYG:

再看一下index.html中的html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>{$name}</title>
    </head>
    <body>
        <br/>
        demo1:大家好,我叫{$name}, 欢迎大家阅读我的smarty学习材料。 
        <br/>
        demo2:大家好,{$str2|cat:"zxb"}, 今天{$str3|date_format:"%Y %m %d "}。 
        <br/>
        demo3:
        <br/>
        {html_checkboxes name="CheckBox" options=$CheckName selected=$IsChecked   separator=""}
        <br/>
        demo4:
        {html_options name=foo options=$lookups selected=$fav}
        <br/>
        demo5:<input type="text" value={$txtvalue} />
        <br/>
        demo6:
        <table>
            {section name=loop loop=$News step=1}
            <tr bgcolor="{cycle values="#D4D0C8,#cccccc"}">

                {if 
                $smarty.section.loop.index 
                % 
                2==0}

            </tr>
            <tr>
                {/if}
                <td>
                    {$News[loop].newsID}
                </td>
                <td>
                    {$News[loop].newsTitle}
                </td>
            </tr>
            {/section}
        </table>
        <hr>
        <center>
            CopyRight(C) by 大师兄 2004年8月 Email: teacherli@163.com 
        </center>
        <hr>
    </body>
</html>

在index.php定义变量:

<?php
require   'class/Smarty.class.php';

$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->debugging = false;
$smarty->caching = false;
$smarty->cache_lifetime = 120;

 
$smarty->assign("str2", "我的名字叫:");  
$smarty->assign("str3", "");
$smarty->assign("name", "dddd",true);  

$smarty->assign('CheckName', array(
		1001 => '语文',
		1002 => '数学',
		1003 => '外语'));
$smarty->assign('IsChecked', 1003);

$arr['Sport'] = array(6 => 'Golf', 9 => 'Cricket',7 => 'Swim');
$arr['Rest'] = array(3 => 'Sauna',1 => 'Massage');
$smarty->assign('lookups', $arr);
$smarty->assign('fav', 7);
$smarty->assign('txtvalue', "wodetxt");



$array1[]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
$array1[]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
$array1[]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
$array1[]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
$array1[]= array("newsID"=>"005", "newsTitle"=>"第5条新闻");
$array1[]= array("newsID"=>"006", "newsTitle"=>"第6条新闻");
$array1[]= array("newsID"=>"007", "newsTitle"=>"第7条新闻");
$array1[]= array("newsID"=>"008", "newsTitle"=>"第8条新闻");


$smarty->assign("News", $array1);

$smarty->display('index.html');
 
 


展示效果如图:

 


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值