当前位置: 移动技术网 > IT编程>开发语言>PHP > Smarty模板常见的简单应用分析

Smarty模板常见的简单应用分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例分析了smarty模板常见的简单应用。分享给大家供大家参考,具体如下:

首先要将smarty这个类包含进来:

include_once '../libs/smarty.class.php';

然后创一个smarty对象:

$smarty = new smarty;

可以自定义smarty 的开始结束符,默认为{ }

$smarty->left_delimiter ='<<'; //左符号为 <<
$smarty->right_delimiter='>>'; //右符号 为 >>

最重要方法的好像是assign,如:

$smarty->assign('test',$te);
//将$test的值付给test,在模板页tpl中显示用{$test}

又如$arr=array(1,2,3);赋值仍是这样:

$smarty->assign('arr',$arr);

但在模板页显示时要借助foreach 或 section,foreach 用法如下:

{foreach item=item from=$arr key=ke name=foe}
 $item
{/foreach}
//此处的$item相当于$arr[$ke],foreach序列化{$smarty.foreach.foe.iteration}

而section 用法如下:

{section name='test' loop=$arr}
{$smarty.section.name.iteration}//使输出序列化,序号从1开始,index从0开始
{$arr[test]}
{/section}

最后最重要的一步操作千万不要忘记那就是:

$smarty->display('test.tpl');

下面说一些常用东西怎么样的在模板上显示

1.连接操作:

我叫{$str1|cat:"李白"};//输出结果就是:我叫 $str1 李白

2.当前日期:

{$str2|rdate_format:"y%-m%-d%"} //输出结果格式化$str2日期,形如0000-00-00

3.缩进:

{$str3|indent:8:"*"}  //$str3前缩进8个* 默认缩进的是空格

4.大小写:

{$str4|lower} //$str4的小写形式
{$str4|upper} //$str4的大写形式

过滤:

{$url|escape:"url"} //对$url相关特殊字符进行替换
<tr bgcolor='{cycle values="#ebebeb,#acabab"}'>//tr背景交替 颜色分别为#ebebeb,#acabab

匹配替换:

{$str|regex_replace:"~[0-9]~":"asd"} //如果$str匹配[0-9]输出asd

替换

{$str|replace:"net":"com"} //将$str中的net全部替换成com

包含头模板文件:

{include file="top.tpl"}

调用time.inc.php里面的函数:

{insert name="getcurrenttime" assign="current_time" script="time.inc.php"}
  当前时间为{$current_time};
{/insert}

其中time.inc.php内容如下:

<?php
 function smarty_insert_getcurrenttime
 {
  return gmdate('l,j f y g:i a t');//获得当前日期及时间
 }
?>

联系:

mailto{
mailto address="contact@smartyllc.com" subject="smarty llc contact" encode="javascript"}

载入test.conf:

{conf_load file="test.conf" section="test"}
{#tt#}

test.conf内容如下:

[test]
tt = 12122

更多关于smarty相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《php模板技术总结》、《php基于pdo操作数据库技巧总结》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于smarty模板的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网