当前位置: 首页 > 工具软件 > Smarty > 使用案例 >

SMARTY(二)

颜河
2023-12-01

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

 类似资料: