`
348725767
  • 浏览: 104105 次
  • 性别: Icon_minigender_1
  • 来自: 温州
社区版块
存档分类
最新评论

php模板引擎Smarty学习笔记(全)

阅读更多

 

好久没写博客了。最近忙着毕业的事,正在准备着一月份的答辩,系统已经做得差不多了,毕业论文初稿也写得差不多了,接下来估计就是论文修修补补了。本来想要去找工作的,但是学校不准,说是必须要留在学校里搞答辩的事。另外,老师那儿的一些项目还要收尾。就只能再待一个月了。

说到找工作,暑假的时候本打算去杭州找找的,因为那儿的IT技术比较发达,起码比起我现在所在的城市要成熟很多。但是也不知道怎么回事,关键时刻胃疼了,疼的死去活来,差不多疼了一个月。估计是大一,大二的时候通宵次数太多了,对于身体太得瑟了吧。再加上家人的反对,就没去成。有时候,生活并不是事事顺心的;不顺心的时候,要适应变化,不要有悲观情绪,努力挖掘对自己有用的一面。

 

话说学校不让走,一时半会儿也没什么项目做,老师那儿的新项目也不想做了,如果做了估计收尾的时候又要延迟好多天去找工作。就想着学点什么,发现网上都说php做网页速度很快,决定就学php了。

这几天学下来感觉php语法也和java差不多,php有个模板引擎Smarty,感觉和struts里的freemarker模板很像,就下载了个Smarty手册来学习。

接下来是我这几天Smarty的学习笔记(个人能力有限,只能学这么多了-_-,基本用用就差不多了,什么插件,什么内置函数我都没有学,等以后不够用了再学吧):

 

1, 首先是下载Smarty的库文件,附件里有。

2, 解压Smarty库文件,然后记录Smarty-3.1.12\libs\Smarty.class.php的绝对路径。

3, 在项目工程下新建四个文件夹分别是cache(缓存文件)configs(配置文件)templates(模板文件)templates(模板编译出来的文件,一般不理)

 

然后就可以写代码了。附件里有个例子文件demo,里面写着一些基本的Smarty 函数例子。

现在黏贴如下:

 

 

首先贴出配置文件configs\foo.conf。
这里所说的配置文件并不是说的是工程必须的,只是一些变量的值,可以理解为define("xxx","xxx");就是一些常数。

title = "这是配置的标题<br/>"
content = "这是配置的内容<br/>"

 然后是templates\header.tpl

 

 

header<br/>

 

 

然后是templates\index.tpl

 

 

{include file="header.tpl"}


{config_load file="foo.conf"}		<!-- 加载配置文件(定义在configs文件夹下) -->
{$person->name}						<!-- 类变量 -->
{$person->log("哈哈哈这是类<br/>")}		<!-- 类方法 -->
{$name}								<!-- 变量 -->
{$arr["name"]}						<!-- 数组 -->

{#title#}							<!-- 配置文件里的值 -->

{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

{$age|upper}<br/>					<!-- 全大写  全小写(lower)-->
{$age|capitalize}<br/>				<!-- 首字母大写 -->
{$age|count_characters}<br/>		<!-- 字符串个数(不算空格) -->
{$age|count_characters:true}<br/>	<!-- 字符串个数(算空格) -->
{$cat|cat:"后加的字符"}<br/>			<!-- 在字符后面外加字符串 -->
{$no|default:"如果没值,则此字符串为默认"}<br/>			<!-- 默认赋值 -->
{$regex|regex_replace:"/[\r\t\n]/":"3"};<br/>	<!-- 正则替换字符串 -->
{$replace|replace:"123":"abc"}<br/>				<!-- 普通替换 -->
{$tru|truncate:11:"...":true}<br/>				<!-- 截取字符串 -->
{$multi|upper|cat:"123"}<br/>					<!-- 多个修饰 -->

{foreach from=$custid item=curr_id}				<!-- 遍历数组 -->
	{$curr_id}<br/>								<!-- ----- -->
{/foreach}										<!-- ----- -->

{foreach name=outer item=contact from=$contacts}	<!-- 遍历数组 -->
 {foreach key=key item=item from=$contact}			<!-- ----- -->
 {$key}: {$item}<br>								<!-- ----- -->
 {/foreach}											<!-- ----- -->
{/foreach}											<!-- ----- -->

{section name=customer loop=$custid}				<!-- section遍历数组 -->
	{if $smarty.section.customer.first}				<!-- 第一次遍历 -->
 			第一次遍历<br/>								<!-- ----- -->
	{/if}											<!-- ----- -->
	{if $smarty.section.customer.last}				<!-- 最后一次遍历 -->
 			最后一次遍历<br/>							<!-- ----- -->
	{/if}											<!-- ----- -->
	{$smarty.section.customer.index}:{$custid[customer]}<br>						
{sectionelse}										<!-- ----- -->
	there are no values in $custid.					<!-- ----- -->
{/section}											<!-- ----- -->
	
{if $name eq "这是变量<br/>"}							<!-- 判断语句 -->
	true<br/>										<!-- ----- -->
{else}												<!-- ----- -->
	false<br/>										<!-- ----- -->
{/if}												<!-- ----- -->

{ldelim}{rdelim}<br/>								<!-- 输出"{}" -->

{assign var="name" value="Bob"}						<!-- 自定义变量 -->
$name的自定义值为 {$name}.<br/>							<!-- ----- -->

{counter start=0 skip=2 print=false}				<!-- 自增数字 -->
{counter}<br/>										<!-- ----- -->
{counter}<br/>										<!-- ----- -->
{counter}<br/>										<!-- ----- -->
{counter}<br/>										<!-- ----- -->

{fetch file="http://localhost/Hello/demo/login.php"}<!-- 载入界面 -->

 然后是index.php

 

<?php

require('C:\Users\cng\Downloads\Smarty-stable\Smarty-3.1.12\libs\Smarty.class.php');

class Person
{
	public $name;
	function  __construct()
	{
		$name="cng";
	}
	
	function log($name)
	{
		print $name;
	}
}

$person=new Person();
$person->name="这是载入的类<br/>";

$smarty = new Smarty;

$smarty->assign("person",$person);
$smarty->assign("name","这是变量<br/>");
$smarty->assign("arr",array("name"=>"这是数组<br/>"));
$smarty->assign("age","cng");
$smarty->assign("cat","后面会加字符:");
$smarty->assign("regex","正则替换\n哈哈哈");
$smarty->assign("replace","普通替换123哈哈哈");
$smarty->assign("tru","我是正文哈哈哈哈哈");
$smarty->assign("multi","abc");
$smarty->assign("custid",array("chen","nai","gong"));
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
		array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));



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


?>

 最后是login.php

<?php print "这是login界面";
?>

 这些就是Smarty模板引擎的基本使用,基本上看了代码之后就知道怎么用了。

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics