Smarty

Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是要使PHP程序员同前端人员分离,使程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。

一. 安装

  下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来演示给大家一个安装实例,看过应该会举一反三的。

  (1) 根目录下建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, configs/.

      (2)建一个setup.php文件,配置文件:

<?php
require('class/Smarty.class.php');
define('_ROOTPATH','e:/1688/smarty');
$php_sm = new Smarty();
$php_sm->template_dir = _ROOTPATH."/templates/";
$php_sm->compile_dir =_ROOTPATH."/templates_c/";
$php_sm->config_dir = _ROOTPATH."/configs/";
$php_sm->cache_dir = _ROOTPATH."/cache/";
$php_sm->left_delimiter = '<{';
$php_sm->right_delimiter = '}>';
?>

(3)建一个模板文件,放到smarty下,如:forum.php

<?php
require('includes/setup.php');//上面那个setup.php放到了smarty文件夹下的includes文件夹中
$forum=array(
 array("category_id"=>1, "category_name"=>"科技",
    "topic"=>array(
      array("topic_id"=>1, "topic_name"=>"计算机"),
      array("topic_id"=>2, "topic_name"=>"天文学")
   )
 ),
 array("category_id"=>2, "category_name"=>"娱乐",
   "topic"=>array(
         array("topic_id"=>1, "topic_name"=>"周星星"),
         array("topic_id"=>2, "topic_name"=>"周时发"),
         array("topic_id"=>3, "topic_name"=>"周公"),
         array("topic_id"=>4, "topic_name"=>"周欣儿")
   )
 ),
 array("category_id"=>3, "category_name"=>"文化",
   "topic"=>array(
        array("topic_id"=>1, "topic_name"=>"good") 
   )    
 )
);
$php_sm->assign(array("title"=>"BBS2011", "forum"=>$forum));
$php_sm->display('arr.htm');//arr.htm文件放到smarty文件夹下的templates文件夹中
?>

(4)显示文件,arr.htm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><{$title}></title>
</head>
<body>
<table width="200" border="1" align="center" cellpadding="3"  cellspacing="0">
<{section name=sec1 loop=$forum}>
 <tr>
  <td width="50">|-<{$forum[sec1].category_id}></td>
  <td width="150"><{$forum[sec1].category_name}></td>
 </tr>
 <{section name=sec2 loop=$forum[sec1].topic}>
 <tr>
  <td>&nbsp;---<{$forum[sec1].topic[sec2].topic_id}></td>
  <td>&nbsp;&nbsp;<{$forum[sec1].topic[sec2].topic_name}></td>
 </tr>
 <{/section}>
<{/section}> 
</table>
</body>
</html>

(5)执行forum.php。

 

 

整理一下整个 Smarty 程式撰写步骤:

Step 1. 加载 Smarty 模版引擎。

Step 2. 建立 Smarty 对象。

Step 3. 设定 Smarty 对象的参数。

Step 4. 在程式中处理变量后,再用 Smarty 的 assign 方法将变量置入模版里。

Step 5. 利用 Smarty 的 display 方法将网页秀出。

二、判断

模板文件中可以使用if else等判断语句,即可以将一些逻辑程序放在模板里。"eq","ne", "neq", "gt", "lt","lte", "le", "gte" "ge","is even", "is odd", "is not even", "is notodd", "not", "mod", "div by", "evenby", "odd by","==","!=",">","<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。

  eq相等,

  f7Y"Y f3m @,V+Z }4X0 ne、neq不相等,

  7C E z R S8P t,i:@ ^0 gt大于,

  4d D H4F,Yg;D X)Y0 lt小于,

  gte、ge大于等于,

  lte、le 小于等于,

  not非, mod求模。

  is [not] div by是否能被某数整除,

  ,B z M E m I w0 is [not] even是否为偶数,

  $a is [not] even by $b即($a / $b) % 2 == 0,

  is [not] odd是否为奇,

  $a is not odd by $b即($a / $b) % 2 != 0

  示例:

  {if $name eq"Fred"}

  WelcomeSir.

  {elseif $name eq"Wilma"}

  WelcomeMa'am.

  Welcome,whatever you are.

  {/if}

三、循环

section: http://www.phpjc.cn/smarty/2009/1021/89.html

foreach: http://www.phpjc.cn/smarty/2009/1021/88.html

 

 

### Smarty 模板引擎的基本概念与使用教程 #### ### Smarty 模板引擎概述 Smarty 是一种用于 PHP模板引擎,旨在将应用程序的逻辑层与表现层分离[^1]。通过使用模板文件(`.tpl`),开发者可以专注于页面布局和设计,而无需直接嵌入复杂的 PHP 代码。这种分离方式提高了代码的可维护性和可读性。 #### ### Smarty 的核心功能 - **模板编译**:Smarty 将模板文件编译为 PHP 脚本以提高性能[^1]。 - **变量替换**:支持从 PHP 向模板传递变量,并在模板中动态显示内容。 - **插件扩展**:提供了丰富的插件系统,允许用户自定义函数、修饰符等。 - **缓存机制**:通过缓存输出内容,减少服务器负载并提升响应速度。 #### ### Smarty 的基本使用流程 以下是使用 Smarty 的基本流程: 1. **创建 Smarty 对象**: 首先需要实例化一个 Smarty 对象,并配置相关目录路径。 ```php require_once 'path/to/Smarty.class.php'; $smarty = new Smarty(); $smarty->setTemplateDir('path/to/templates/'); $smarty->setCompileDir('path/to/templates_c/'); $smarty->setCacheDir('path/to/cache/'); $smarty->setConfigDir('path/to/configs/'); ``` 2. **分配变量**: 使用 `assign` 方法将变量从 PHP 脚本传递到模板中。 ```php $smarty->assign('name', 'John Doe'); $smarty->assign('age', 30); ``` 3. **加载模板并显示**: 使用 `display` 方法加载模板文件并渲染输出。 ```php $smarty->display('index.tpl'); ``` 4. **模板文件示例**: 在模板文件中,可以通过 `{}` 标记访问变量。 ```html <!DOCTYPE html> <html> <head> <title>Smarty Example</title> </head> <body> <h1>Hello, {$name}!</h1> <p>You are {$age} years old.</p> </body> </html> ``` #### ### 缓存与性能优化 Smarty 提供了强大的缓存功能,可以通过以下方式启用缓存: ```php $smarty->caching = true; // 启用缓存 $smarty->cache_lifetime = 120; // 设置缓存生命周期为 120 秒 ``` 如果启用了缓存,Smarty 会优先从缓存中读取内容,从而减少对后端逻辑的依赖。 #### ### 插件与自定义功能 Smarty 支持多种插件类型,包括函数、修饰符、块等。以下是一个简单的自定义函数示例: ```php // 自定义函数 exampleFunction function smarty_function_exampleFunction($params, &$smarty) { return "This is a custom function with param: " . $params['param']; } ``` 在模板中调用该函数: ```html {exampleFunction param="test"} ``` #### ### 错误处理与调试 为了方便调试,可以启用 Smarty 的调试模式: ```php $smarty->debugging = true; ``` 启用后,Smarty 会在页面底部显示调试控制台,提供有关模板渲染的信息[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值