目录

Smarty 简单例子

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

Smarty 目录结构

创建以下三个目录结构:

smarty/
smarty_run/
templates/

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  -->