当前位置: 代码迷 >> Web前端 >> Smarty入门与装配过程例子
  详细解决方案

Smarty入门与装配过程例子

热度:629   发布时间:2012-09-06 10:37:01.0
Smarty入门与安装过程例子


?http://www.smarty.net/download

下载...
Latest Stable Release
Smarty 3.1.6 (.tar.gz) (.zip) Dec 1st, 2011
解压之后有两个文件夹
libs(我们需要的文件) demo


只需要拷贝libs到ZendStudio新建的demo下

如现在有一个smartytest项目名称

在smartytest项目新建action configs sys templates
? ? ?smartytest(项目名称)目录如下
?-- action(文件名)
? ? ?? ?--index.php
?-- configs
?? ?--config.php(数据库配置,等)
? ? ?? ?--view.conf
?--runtime
?? ?--cache ?(放编译之后的php)
?-- sys ?(放smarty解压的libs)
?? ?--libs
-- ?templates
?? ?--index.tpl(模板)

?

----------------------------------------action文件夹 -- ----------------------------

----------------------------------------index.php------------------------------

?

<?php

?require '../configs/config.php';

?$smarty->assign('userinfo','this is ok ok ok liangjilong这是我第一个Smarty哦?');

?#--------------数字处理-----------

?$smarty->assign("num1",10);


?$smarty->assign("num2",20);

?#----------------变量调节器---------

?$smarty->assign("zhuanhuan","my name is liangjilong!");//用于首字母大写


?$smarty->assign("xiaoxie","MY NAME IS LIANGJILONG!");//用于转换小写


?$smarty->assign('number', 23.5787446);


?$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");


?$smarty->assign('tagHtml', "<font face=\"helvetica\">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");//strip_tags去除html标签


?$smarty->assign('count_character', 'Cold Wave Linked to Temperatures.');


?$smarty->assign("tpl_cat","this is google addess.."); //串连


?$smarty->assign('tpl_indent', 'NJ judge to rule on nude beach.');#[缩进]

?//$smarty->clear_all_assign();#清除所有赋值


?$data=array(array("one"=>"liang"),array("two"=>"ji"),array("tree"=>"long"));//数组..

?$smarty->assign("var_data",$data);


?//---------------------------if elseif else 使用------------------

?$smarty->assign("var_if","longge");

?$smarty->assign("var_elseif","longshu");

?$smarty->assign("var_else","jilong");


?$smarty->assign("company","hi my name is google");

?$smarty->assign("tpl_escape","hi my name is escape");


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

?>

?

?

?

?

------------------------------------------------连接数据库--------------------------

?

<?php?

require '../configs/config.php';

$mysqli=new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);


$result=$mysqli->query("select * from php_user");


$data=array();


while($row=$result->fetch_assoc()){

$data[]=$row;

}

$smarty->assign("data", $data);

$smarty->display("show.tpl");


?>

-------------------------------------------configs文件夹 ------------------------

?

-------------------------------------------------config.php---------------

?

<?php

/**

?* @Author:liangjilong

?* @Date: 2011-12-10

?* @Encoding:UTF-8

?* @Tags:tags

?* @FileName:config.php

?* @Language PHP

?*/

//"C:/wamp启动php的服务器目录wamp或 apache

define('ROOT', "C:/wamp/www/smarty_cms/"); ?//这个ROOT 变量一定要给引起来?

include ROOT."sys/libs/Smarty.class.php";

header('Content-Type: text/html; charset=utf-8'); //设置编码

#-----------------数据库-----------------#

define('DB_DRIVER', 'mysql');

define('DB_HOSTNAME', 'localhost');

define('DB_USERNAME', 'root');

define('DB_PASSWORD', 'root');

define('DB_DATABASE', 'php-test');

define('DB_PREFIX', 'php_');

#----------------------------------------#

$smarty = new Smarty;

$smarty->template_dir=ROOT."templates";//模板

? ? $smarty->config_dir=ROOT."configs";//配置

$smarty->cache_dir=ROOT."cache";//缓存

#--------------设置缓存---------

$smarty->caching = true;

? $smarty->caching = 2;#lifetime is per cache

$smarty->cache_lifetime = 60; ?//缓存1分钟

#--------------设置缓存---------

$smarty->compile_dir=ROOT."runtime/cache";//编译之后的文件

/**

* 预设的标示符号为 { 及 },但为了中文冲码及 Javascript 的关系

*/

$smarty->left_delimiter="<{";

$smarty->right_delimiter="}>";?

?>



?

?

-------------------------setup.conf---------------

?

border=20

tabcolor=darkgray

bgcor=#EDDFF

tab_color=gray

tr_col=gray

tabfooter=red

tabwidth=400

?

?

?

------------------------setup.conf----------------------------

?

emphstart = <b>

emphend = </b>

title = Welcome to <{$company}>'s home page!

ErrorCity = You must supply a <{#emphstart#}>city<{#emphend#}>.

ErrorState = You must supply a <{#emphstart#}>state<{#emphend#}>.

?

?

------------------------templates文件夹- ---------------------------

?

------------------------footer.tpl----------------------------

?

<p>

<table align="center">

? <tr>

? ? <td><a href="http://www.smarty.net/">smarty官方网</a></td>

?

? ? <td><a href="http://www.smarty.net/">联系我们</a></td>

? </tr>

</table>

</body>

</html>

?

------------------------header.tpl----------------------------

?

<html>

<head>

<title>smarty应用.</title>

</head>

<body>

?


------------------------index.tpl----------------------------

?

?

<{**------引入头文件.--------*}>

<{include file="header.tpl"}>

<{*****引入函数的配置*****}>?

<{config_load file="view.conf" section="three"}>


<{config_load file="setup.conf"}>


<table align="center" border="1" bgcolor="<{#tabcolor#}>">

<tr align="center" background="{#bgcor#}">

<td colspan="2">Smarty应用啊?哦</td>

</tr>

<tr>

<td style="color: <{#bgcor#}>">你好,<{$userinfo}>!</td>

<td>O(∩_∩)O哈哈~</td>

</tr>

<tr>

?<{*Attributes属性..*}>

<td>时间:<{html_select_date display_days=yes start_year="-115" end_year="+115" month_format="%m" field_order="YMD" }></td>

<td></td>

</tr>

<tr>

<{*-----------数学运算-----*}>

<td>10+1结果:<{$num1+1}> &nbsp;&nbsp;&nbsp;10*20结果: <{$num1*$num2}></td>

<td>truncate截取:<{$num1|truncate:"`$num2/$num1-1`"}></td>

</tr>

<tr>

<td>首字母大写:<{$zhuanhuan|capitalize}></td>

<td>全部大写<{$zhuanhuan|upper}></td>

</tr>

<tr>

<{*----------截取五个字节,其中有三个字符被...代替-----------*}>

<td>truncate截取:<{$zhuanhuan|truncate:5:"..."}></td>

<td><{$xiaoxie|lower}></td>

</tr>

<tr>

<td>string_format的使用:<{$number|string_format:"%.2f"}>&nbsp;&nbsp;<{$number|string_format:"%d"}></td>

<td>

<{$articleTitle}>

<{$articleTitle|wordwrap:30}>

<{$articleTitle|wordwrap:20}>

<{$articleTitle|wordwrap:30:"<br>\n"}>

<{$articleTitle|wordwrap:30:"\n":true}>

</td>

</tr>

<tr>

<td>strip_tags去除html标签<{$tagHtml|strip_tags}></td>

<td>count_characters:<{$count_character|count_characters}></td>

</tr>

<tr>

<td>cat:<{$tpl_cat|cat:" 好的"}></td>

<td>

<{$tpl_indent}>

? ? <{$tpl_indent|indent}>

? ? <{$tpl_indent|indent:10}>

<{$tpl_indent|indent:1:"\t"}>

</td>

</tr>

<tr>

<td>

? ?php标签与smarty结合用:

<?php echo $userinfo?>

</td>

<td>

<{mailto address="haha@liangjilong.com"}>

<{mailto address="haha@liangjilong.com" text="send me some mail"}>

<{mailto address="haha@liangjilong.com" encode="javascript"}> ?

<{mailto address="haha@liangjilong.com" encode="hex"}> ?<{*加密过的*}>

<{mailto address="haha@liangjilong.com" subject="Hello to you!"}>

<{mailto address="haha@liangjilong.com" cc="you@domain.com,they@domain.com"}>

<{mailto address="haha@liangjilong.com" extra='class="email"'}>

?

</td>

</tr>

<tr>

?<td>

捕获模板内容

<{*========捕获模板内容 =======*}>

<{capture name=banner}>

<{include file="get_banner.tpl"}>

<{/capture}>

<{if $smarty.capture.banner eq ""}>

</td>

<td>

<{$smarty.capture.banner}>

</td>

? <{/if}>

</tr>

<tr>

<td>foreache循环

<br>

<{foreach name=outer item=contact from=$var_data}>

<{foreach key=key item=item from=$contact}>

<{$key}>: <{$item}><br>

<{/foreach}>

<{/foreach}>

</td>

<td>

if elseif else使用<br>

<{if $var_if eq "longge"}>

Welcome Sir.

<{elseif $var_elseif eq "longshu"}>

Welcome Ma'am.

<{else}>

Welcome, whatever you are.

<{/if}>

</td>

</tr>

<tr>

<td>

<{eval var=$company}><br>

<{eval var=#title#}><br>

<{eval var=#ErrorCity#}><br>

<{eval var=#ErrorState# assign="state_error"}><br>

<{$state_error}>

</td>

<td>

<{$tpl_escape|escape}><br>

<{$tpl_escape|escape:"html"}><br>

<{$tpl_escape|escape:"htmlall"}><br>

<{$tpl_escape|escape:"url"}><br>

<{$tpl_escape|escape:"quotes"}><br>

<a href="mailto:<{$tpl_escape|escape:"hex"}>"><{$tpl_escape|escape:"hexentity"}></a>

</td>

</tr>

</table>

<{include file="footer.tpl"}>


?

------------------------show.tpl----------------------------

?

?

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http//www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>php的查询</title>

</head>

<{********************导入配置文件*******************}>

<{config_load file="view.conf" section="three"}>

<body>

<table align="center" border="1" width="800">

<caption><h1>section循环</h1></caption>

<tr>

<td align="center">ID</td>

<td align="center">username</td>

<td align="center">address</td>

<td align="center">tel</td>

<td align="center">content</td>

</tr>

<tr bgcolor="<{#tr_col#}>">

<th>编号</th>

<th>姓名</th>

<th>地址</th>

<th>电话</th>

<th>内容</th>

</tr>

<{**********************section循环********************}>

<{section loop=$data name=ls}>

<tr>

<td><{$data[ls].id}></td>

<td><{$data[ls].username}></td>

<td><{$data[ls].address}></td>

<td><{$data[ls].tel}></td>

<td><{$data[ls].content}></td>

</tr>

<{sectionelse}>

<tr><td colspan="5">没有相关信息</td></tr>

<{/section}>?

</table>


<p>



<{**********************foreach循环********************}>

<table align="center" border="1" width="800">

<tr align="center">

<td colspan="6">foreach循环</td>

</tr>


<{foreach from=$data item="row" name="outer"}>

<{if $smarty.foreach.outer.iteration is even }>

<tr bgcolor="<{#tr_col#}>">

<{elseif $smarty.foreach.outer.first}>

<tr bgcolor="<{#bgcor#}>">

<{elseif $smarty.foreach.outer.last}>

<tr bgcolor="blue">

<{else}>

<tr>

<{/if}>

<td><{$smarty.foreach.outer.iteration}></td>

<{foreach from=$row item="col" name="inner"}>

<td> <{$col}></td>

<{/foreach}>

</tr>

<{foreachelse}>

没有相关的数据

<{/foreach}>


</table>

</body>

</html>

-------------------------------------------SQLScript---------------------------------
/*
MySQL Data Transfer
Source Host: localhost
Source Database: php-test
Target Host: localhost
Target Database: php-test
Date: 2011/12/17 9:55:39
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for php_user
-- ----------------------------
CREATE TABLE `php_user` (
? `id` int(11) NOT NULL auto_increment,
? `address` varchar(255) NOT NULL,
? `username` varchar(255) NOT NULL,
? `tel` text NOT NULL,
? `content` text NOT NULL,
? PRIMARY KEY ?(`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records?
-- ----------------------------
INSERT INTO `php_user` VALUES ('13', '', 'longge', '', '');
INSERT INTO `php_user` VALUES ('14', '', 'longge', '', '');
INSERT INTO `php_user` VALUES ('2', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('3', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('4', '11114444', '龙哥', '111111333', '4444');
INSERT INTO `php_user` VALUES ('5', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('6', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('7', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('8', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('9', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('10', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('11', '广东阳春', '龙叔', '7080808080', '今天好天气');
INSERT INTO `php_user` VALUES ('12', '1111', 'wangzi', '111111', '1111');
INSERT INTO `php_user` VALUES ('15', '', 'longge', '', '');
INSERT INTO `php_user` VALUES ('16', '', 'longge', '', '');
INSERT INTO `php_user` VALUES ('17', '', 'longge', '', '');



?



?