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>\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', '', '');



相关推荐
Smarty是一个专为PHP设计的模板引擎,它的主要目标是实现应用程序的逻辑与界面设计的分离,使得程序员和美工能够独立工作,互不影响。在PHP项目中,特别是多人协作的大型项目,Smarty的重要性尤为突出。 Smarty的...
为了更好的配合教学工作,提高教学效率与学生学习的积极性, 特意制作了简易的 MVC 例子和 Smarty 模板引擎,帮助同学更快掌握 brophp 框架和相关的 php 编程知识,达到抛砖引玉的效果。
Smarty是一种模板引擎,用于实现MVC(模型-视图-控制器)设计模式中的视图部分,它允许将应用程序的业务逻辑与设计界面分离。在PHP开发中,Smarty能够提高代码的可维护性,减少重复代码,同时加速开发流程。本文会...
#### 一、PHP简介与安装配置 - **PHP定义**:PHP(Hypertext Preprocessor)是一种广泛使用的开源通用脚本语言,尤其适用于Web开发并可嵌入HTML中。语法吸收了C语言、Java以及Perl的特点,利于学习,使用灵活,支持...
Smarty模板引擎是一个专门为PHP设计的模板引擎,它的主要目的是实现应用程序的MVC(Model-View-Controller)架构,使得程序的逻辑与用户界面能够清晰地分离。MVC模式强调数据处理(模型)、用户界面(视图)和控制...
Smarty是一款使用PHP编写的模板引擎,它的主要目的是实现程序逻辑与界面设计的分离,使得程序员和美工能够各自独立工作,避免相互影响。这个技术对于大型项目尤其有用,因为它提高了开发效率并简化了维护。 Smarty...
这些例程可能涵盖了上述各个知识点,每个例子都有详细的步骤和解释,帮助读者将理论知识转化为实践技能。在学习过程中,读者可以通过阅读代码、运行实例、调试错误,逐步建立起对PHP编程的直观认识。 总之,《目前...
9. **模板引擎**:虽然初学者例子可能不包含,但实际项目中,为了分离业务逻辑和视图,可以使用模板引擎,如Twig或Smarty,使代码更易维护。 10. **RESTful API设计**:现代Web应用倾向于采用API接口,这个例子可能...
10. **模板引擎**:可能简述如何使用模板引擎(如Smarty)将业务逻辑与视图层分离,提高代码可维护性。 11. **实战示例**:通过实际的Web应用例子,演示如何使用PHP处理表单数据、显示动态内容等。 通过这个第二章...
PHP模板引擎Smarty入门使用 - **知识点**: Smarty是一种广泛使用的PHP模板引擎。本节介绍了Smarty的基本用法,包括模板文件的创建、变量的传递等。 - **学习目标**: 学会使用Smarty模板引擎,提高页面渲染效率。 #...
...它以其易学易用、功能强大、跨平台等特性,深受开发者喜爱。... ... ...熟练运用这些语法结构是编写高效代码的关键。...在学习过程中,建议逐一运行这些例子,分析其运行逻辑,并尝试修改和扩展,以增强实际应用能力。
在实际项目中,开发者可能会使用模板引擎(如Twig或Smarty)来分离视图和逻辑。例子可能包含如何集成和使用模板引擎,提高代码的可维护性。 10. **框架应用** PHP有许多流行框架,如Laravel、Symfony和Yii,它们...
10. PHP模板引擎:源码可能包含使用PHP模板引擎(如Smarty)的例子,使视图和逻辑分离,提高代码可读性和可维护性。 11. PHP与HTML/JavaScript/CSS集成:了解如何在PHP中生成动态HTML,以及与JavaScript和CSS的交互...
10. **模板引擎**:PHP常常与模板引擎结合,如Smarty,用于分离业务逻辑和视图展示。通过实例,你可能了解到如何使用模板引擎提高代码的可维护性。 11. **PHP函数库**:PHP内置了大量有用的函数,如日期时间处理、...
12. **模板引擎**:例如`Smarty`,它可以帮助分离业务逻辑和视图展示,提高代码可维护性。 13. **PHP框架**:如`Laravel`、`CodeIgniter`等,它们提供了一套完整的开发结构和工具,简化开发流程。 14. **安全**:...
通过实际例子,如开发一个简单的计算器程序,学员可以进一步巩固对条件分支的理解,并学会如何在实际开发中灵活应用这些结构。 教程并未止步于此,它还计划探讨PHP的高级主题,比如数据库操作、PDO数据库抽象层、...