Smarty入門とインストール手順の例
18082 ワード
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}> 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"}> <{$number|string_format:"%d"}></td>
<td>
<{$articleTitle}>
<{$articleTitle|wordwrap:30}>
<{$articleTitle|wordwrap:20}>
<{$articleTitle|wordwrap:30:"<br>
"}>
<{$articleTitle|wordwrap:30:"
":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="[email protected]"}>
<{mailto address="[email protected]" text="send me some mail"}>
<{mailto address="[email protected]" encode="javascript"}>
<{mailto address="[email protected]" encode="hex"}> <{* *}>
<{mailto address="[email protected]" subject="Hello to you!"}>
<{mailto address="[email protected]" cc="[email protected],[email protected]"}>
<{mailto address="[email protected]" 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', '', '');