用户工具

站点工具


php:smarty:simple-example

Smarty 简单例子

下面的例子是基于Smarty 2.6版本的,可以不适用与Smarty 3。

Smarty 目录结构

创建以下三个目录结构:

smarty/
smarty_run/
templates/
  • smarty: smarty库文件
  • smarty_run: smarty临时文件
  • templates: .tpl模板文件

php脚本设置

require("smarty/libs/Smarty.class.php");
$smarty = new Smarty;
$smarty->config_dir="smarty/libs/Config_File.class.php";
$smarty->caching=false; 
$smarty->cache_dir="smarty_run/cache/";
$smarty->template_dir="templates";
$smarty->compile_dir="smarty_run/templates_c/";
$smarty->left_delimiter = "{{";
$smarty->right_delimiter = "}}";
 
// ...
// ...
 
// pass variables to smarty
$url = "http://www.pythonclub.org/";
 
$smarty->assign('title',  $title);
$smarty->assign('url',  $url);
$smarty->assign('post',  $post);
$smarty->assign('id',  $id);
$smarty->display('post.tpl');

tpl模板文件

<!-- post content start -->
<div class="content-body">
 
  <!-- title -->
  <div class="custom-title  text-left"><h2 class="title">{{$title}}</h2><p class="sub_title"><br></p></div>
 
  <!-- rich content area -->
  <div class="custom-richtext">
      <font size="4">
      {{ $post }}
      </font><hr>
      <div style="clear:both"></div>
</div>
<!-- post content end  -->
php/smarty/simple-example.txt · 最后更改: 2014/05/26 05:02 (外部编辑)