SMARTY(二)

来源:互联网 发布:工商局网络与合同监管 编辑:程序博客网 时间:2024/06/02 14:05

SMARTY

声明$template_dir属性,保存模板文件所在路径

声明$complile_dir属性,保存编译后文件所在路径

$template_dir="./templates";//模板文件所在路径

  $complile_dir="./templates_c";//生成文件所在路径

  $tpl_vars=array();

public function __construct($template_dir="./templates",$compile_dir="./templates_c"){
   $this->template_dir=rtrim($template_dir,'/').'/';
   $this->compile_dir=rtrim($compile_dir,'/').'/';
   $this->tpl_vars=array();
     } 

如何实现将php文件中声明的变量分配到html(tpl)文件?

需要两个参数
$tpl_vars------出现在模板文件(*.html、*.tpl)中变量的名称
$value------模板文件中对应变量的值,该值来自于php文件


实现模板文件的调用


display("模板文件名")


第一步:从模板文件中获取<{$titlename}>结构


第二步:替换成<?php echo $titlename;?>语法


定义模板文件中<{ $title }>结构的正则表达式


$pattern = '/\<\{\s*\$([a-zA-Z_\x7f\xff][0-9a-zA-Z_\x7f\xff]*)\s*\}\>/i';

      $replacement='<?php echo $this->tpl_vars["${1}"];?>';

      将变量$repcontent写入到com_a.php文件

原创粉丝点击