无为清净楼资源网 Design By www.qnjia.com

本文实例讲述了smarty模板引擎基础知识。分享给大家供大家参考。具体如下:

一、基本概念

1.什么是mvc"htmlcode">

<"title","hello minismarty!");
  $miniSmarty->assign("content","<font color='red'>this is content!</font>");
  //传递数据到哪个页面显示
  $miniSmarty->display("intro.tpl");
"htmlcode">
<!--这是个模板文件--> 
<html> 
<head> 
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{$title}</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
{$content}
</body>
</html>

这里面的内容是用占位符的形式,smarty的作用就是将占位符的内容替换成真正的数据。
这样就可以实现模板文件和数据文件强制分离,通过smarty进行数据的传递。

4.编写cls_MiniSmarty.php文件

<"./templates/"; 
  //模板文件被替换后的文件路径 
  var $templates_c_dir = "./templates_c/"; 
  //存放变量值 
  var $tpl_vars = array (); 
  //主要模拟2个方法 
  /** 
   * 添加数据 
   * 参数1:键 
   * 参数2:值,默认为null 
   */ 
  function assign($tpl_var, $var = null) { 
    if ($tpl_var != '') { 
      $this->tpl_vars[$tpl_var] = $var; //将数据添加到数组中 
    } 
  } 
  /** 
   * 显示数据 
   * 参数1:显示到哪个模板文件中 
   */ 
  function display($tpl_file) { 
    //获得模板文件的路径 
    $tpl_file_path = $this->template_dir . $tpl_file; 
    //获得模板文件被编译后的文件路径 
    $compile_file_path = $this->templates_c_dir . "com_" . $tpl_file . ".php"; 
    //判断文件是否存在 
    if (!file_exists($tpl_file_path)) { 
      return false; 
    } 
    //不用每次都生成编译文件,只有编译文件不存在或者模板文件被修改了才生成新的编译文件 
    //相当于缓存了编译文件 
    //filemtime函数:获得文件的生成时间 
    if (!file_exists($compile_file_path) || filemtime($tpl_file_path) > filemtime($compile_file_path)) { 
      //读取模板文件的内容 
      $fpl_file_content = file_get_contents($tpl_file_path); 
      $newStr = myReplace($fpl_file_content); 
      //将替换后的字符串生成新的文件,也就是编译后的文件 
      file_put_contents($compile_file_path, $newStr); 
    } 
    //引入编译后的文件 
    include $compile_file_path; 
  } 
  /** 
   * 对模板文件中的内容进行替换,获得新的字符串 
   */ 
  function myReplace($fpl_file_content) { 
    $pattern = array ( 
      '/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i' 
    ); 
    $replace = array ( 
      '<"${1}"] "//img.jbzj.com/file_images/article/201503/201533094419324.png" alt="" />

结论:

真正运行的文件,既不是index.php,也不是intro.php,而是二者通过smarty作用后的文件:
com_intro.tpl.php。这个文件中数据来源于index.php,显示的布局来自intro.tpl,中间的桥梁是smarty。
smarty的作用是接受数据、填充数据(替换模板中的占位符)、并加载替换后的文件。

三、讲解smarty使用细节

1.如何配置smarty"htmlcode">

$smarty->left_delimiter="{<";
$smarty->right_delimiter=">}";

③smarty的一些基本配置

$smarty->template_dir="./templates";//模板路径
$smarty->compile_dir="./templates_c";//编译路径
$smarty->caching=false;  //是否使用缓存
$smarty->cache_dir="./smarty_cache";//如果使用缓存的话:缓存的路径

3.smarty模板技术分配变量的细节问题

一句话:可以分配php支持的各种数据。
php基本数据:int double string bool
复合数据类型:array object
特殊数据类型:resource null

希望本文所述对大家的php程序设计有所帮助。

标签:
smarty,模板引擎,基础知识

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?