smarty代码

<?php

class iSmarty
{
 /*
     * The name of the directory where templates are located.
     * @var string
     */
 var $templatedir  = 'template/';

 /*
     * The directory where compiled templates are located.
     * @var string
     */
 var $compiledir   = 'compile/';

 /*
     * where assigned template vars are kept
     * @var array
     */
 var $vars         = array();

 /*
     * compile a resource
     * sets PHP tag to the compiled source
     * @param string $tpl (template file)
     */
 function parse($tpl)
 {
  // load template file //
  $fp   = @fopen($tpl, 'r');
  $text = fread($fp, filesize($tpl));
  fclose($fp);
  // repalce template tag to PHP tag //
  $text = str_replace('{/if}','<?php } ?>',$text);
  $text = str_replace('{/loop}','<?php } ?>',$text);
  $text = str_replace('{foreachelse}','<?php } else {?>',$text);
  $text = str_replace('{/foreach}','<?php } ?>',$text);
  $text = str_replace('{else}','<?php } else {?>',$text);
  $text = str_replace('{loopelse}','<?php } else {?>',$text);
  // template pattern tags //
  $pattern = array(
   '//$(/w*[a-zA-Z0-9_])/',
   '//$this/-/>vars/[/'(/w*[a-zA-Z0-9_])/'/]+/.(/w*[a-zA-Z0-9])/',
   '//{include file=(/"|/'|)(/w*[a-zA-Z0-9_/.][a-zA-Z]/w*)(/"|/'|)/}/',
   '//{/$this/-/>vars(/[/'(/w*[a-zA-Z0-9_])/'/])(/[/'(/w*[a-zA-Z0-9_])/'/])?/}/',
   '//{if (.*?)/}/',
   '//{elseif (.*?)/}/',
   '//{loop /$(.*) as (/w*[a-zA-Z0-9_])/}/',
   '//{foreach /$(.*) (/w*[a-zA-Z0-9_])/=/>(/w*[a-zA-Z0-9_])/}/');
  // replacement PHP tags //
  $replacement = array(
   '$this->vars[/'/1/']',
   '$this->vars[/'/1/'][/'/2/']',
   '<?php $this->display(/'/2/')?>',
   '<?php echo /$this->vars/1/3?>',
   '<?php if(/1) {?>',
   '<?php } elseif(/1) {?>',
   '<?php if (count((array)/$/1)) foreach((array)/$/1 as /$this->vars[/'/2/']) {?>',
   '<?php if (count((array)/$/1)) foreach((array)/$/1 as /$this->vars[/'/2/']=>$this->vars[/'/3/']) {?>');
  // repalce template tags to PHP tags //
  $text = preg_replace($pattern,$replacement,$text);

  // create compile file //
  $compliefile = $this->compiledir.basename($tpl).'.php';
  if ($fp = @fopen($compliefile, 'w')) {
   fputs($fp, $text);
   fclose($fp);
  }
 }

 /*
     * assigns values to template variables
     * @param array|string $k the template variable name(s)
     * @param mixed $v the value to assign
     */
 function assign($k,$v=null)
 {
  $this->vars[$k] = $v;
 }
 
 /*
     * ste directory where templates are located
     * @param string $str (path)
     */
 function templateDir($path)
 {
  $this->templatedir = $this->pathCheck($path);
 }

 /*
     * set where compiled templates are located
     * @param string $str (path)
     */
 function compileDir($path)
 {
  $this->compiledir = $this->pathCheck($path);
 }

 /*
     * check the path last character
     * @param string $str (path)
  * @return string
     */
 function pathCheck($str)
 {
  return (preg_match('///$/',$str)) ? $str : $str.'/';
 }

    /*
     * executes & displays the template results
     * @param string $tpl (template file)
     */
 function display($tpl)
 {
  $tplfile = $this->templatedir.$tpl;
  if (!file_exists($tplfile)) {
   exit('can not load template file : '.$tplfile);
  }
  $compliefile = $this->compiledir.$tpl.'.php';
  if (!file_exists($compliefile) || filemtime($tplfile) > filemtime($compliefile)) {
   $this->parse($tplfile);
  }
  include_once($compliefile);
 }
}

?> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值