php smarty简单的例子

实例1:

先来看一个简单的例子。
=====================================================
index.tpl
=====================================================



{* 显示是smarty变量识符里的用*包含的文字为注释内容 *}
{include file="header.tpl"}{*页面头*}
      大家好,我叫{$name}, 欢迎大家阅读我的smarty学习材料。
{include file="foot.tpl"}{*页面尾*}   


上边的这个例子是一个tpl模板,其中:
1. {**}是模板页的注释,它在smarty对模板进行解析时不进行任何输出,仅供模板设计师对模板进行注释。
2. {include file="xxx.tpl"}使用此句将一个模板文件包含到当前页面中,例子中将在网站中公用事的head.tpl与foot.tpl进行了包含,你可以
这样想,使用这一句将xxx.tpl中的内容全部复制在当前语句处。当然,你不使用这一句也可以,将XXX.tpl中的内容复制到当前语句处
也是完全可以了。
3.{$name}: 模板变量,smarty中的核心组成,采用smarty定义的左边界符{与右边界符}包含着、以PHP变量形式给出,在smarty程序中将使用
$smarty->assign("name", "疯癫";将模板中的$name替换成“疯癫”三个字。

整个实例源程序如下:
=============================
header.tpl
=============================


<html>
<head>
   <title>smarty教程</title>
</head>
   <body>

===============================
foot.tpl
===============================


<hr>
    <center> CopyRight(C) by 2007年12月</center>
<hr>
</body>
   </html>


=====================================================
index.tpl
=====================================================

{* 显示是smarty变量识符里的用*包含的文字为注释内容 *}
{include file="header.tpl"}{*页面头*}
      大家好,我叫{$name}, 欢迎大家阅读我的smarty学习材料。
{include file="foot.tpl"}{*页面尾*}   


================================================
index.php
================================================
<?php

   include_once("./comm/Smarty.class.php"); //包含smarty类文件

   $smarty = new Smarty();   //建立smarty实例对象$smarty
   $smarty->templates("./templates"); //设置模板目录
   $smarty->templates_c("./templates_c"); //设置编译目录
  
   //----------------------------------------------------
   //左右边界符,默认为{},但实际应用当中容易与JavaScript
   //相冲突,所以建议设成<{}>或其它。
   //----------------------------------------------------
   $smarty->left_delimiter = "{";
   $smarty->right_delimiter = "}";

   $smarty->assign("name", "疯癫"); //进行模板变量替换
  
   //编译并显示位于./templates下的index.tpl模板
   $smarty->display("index.tpl");
?>


最终执行这个程序时将显示为:
================================
执行index.php
================================
<html>
<head>
   <title>smarty教程</title>
</head>
<body>
    大家好,我叫疯癫, 欢迎大家阅读我的smarty学习材料。
    <hr>
    <center> CopyRight(C) by 2007年12月</center>
<hr>
</body>
   </html>

the second part

这个例子是综合使用smarty模板参数的一个例子,这些参数用来控制模板的输出,我只选其中几个,其它的参数你去看参考吧。

================================================
exmple2.tpl
================================================


<html>
   <head><title>smarty示例2</title></head>
   <body>
       1. 第一句首字母要大写:{$str1|capitalize}<br>
       2. 第二句模板变量 + 疯癫:{$str2|cat:"疯癫"}<br>
       3. 第三句输出当前日期:{$str3|date_format:"%Y年%m月%d日"}
       4. 第四句.php程序中不处理,它显示默认值:{$str4|default:"没有值!"}
       5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:<br>
      {$str5|indent:8:"*"}}<br>
       6. 第六句把hd_lj@163.com全部变为小写:{$str6|lower}<br>
       7. 第七句把变量中的Crazy Hand替换成:疯癫:{$str7|replace:"Crazy Hand":"疯癫"}<br>
8. 第八句为组合使用变量修改器:{$str8|capitalize|cat:"这里是新加的时间:"|date_format:"%Y年%m月%d日"|lower}
   </body>
</html>


===============================================
example2 .php
===============================================
<?php

   include_once("./Smarty.class.php"); //包含smarty类文件

   $smarty = new Smarty();   //建立smarty实例对象$smarty
   $smarty->templates("./templates"); //设置模板目录
   $smarty->templates_c("./templates_c"); //设置编译目录
  
   //----------------------------------------------------
   //左右边界符,默认为{},但实际应用当中容易与JavaScript
   //相冲突,所以建议设成<{}>或其它。
   //----------------------------------------------------
   $smarty->left_delimiter = "{";
   $smarty->right_delimiter = "}";

   $smarty->assign("str1", "my name is Crazy Hand."); //将str1替换成My Name Is Crazy Hand.
   $smarty->assign("str2", "我的名字叫:"); //输出: 我的名字叫:疯癫

     $smarty->assign("str3", "公元"); //输出公元2008年X月X日(我的当前时间)
   //$smarty->assign("str4", ""); //第四句不处理时会显示默认值,如果使用前面这一句则替换为""
   $smarty->assign("str5", "前边8个*"); //第五句输出:********前边8个*
   $smarty->assign("str6", "hd_lj@163.com"); //这里将输出hd_lj@163.com
   $smarty->assign("str7", "this is Crazy Hand"); //在模板中显示为:this is 疯癫
       $smarty->assign("str8", "HERE IS COMBINING:");

   //编译并显示位于./templates下的index.tpl模板
   $smarty->display("example2.tpl");
?>


最终输出效果:
======================================================
example2.php输出效果:
======================================================
<html>
   <head><title>smarty示例2</title></head>
   <body>
       1. 第一句首字母要大写:My Name Is Crazy Hand.<br>
       2. 第二句模板变量 + 疯癫:我的名字叫:疯癫<br>
       3. 第三句输出当前日期:公元2008年X月X日<br>
       4. 第四句.php程序中不处理,它显示默认值:没有值!<br>
       5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:<br>
      ********前边8个*<br>
       6. 第六句把hd_lj@163.com全部变为小写:hd_lj@163.com<br>
       7. 第七句把变量中的Crazy Hand替换成:疯癫:this is 疯癫<br>
8. 第八句为组合使用变量修改器:Here is Combining:这里是新加的时间:2008年X月X日
   </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值