1. [PHP]代码
tVal = array();
}
/**
* 设置模版文件目录
* @param string $dir
*/
public function setTemplateDir($dir) {
$this->tDir = $dir;
}
/**
* 是否实时编译
* @param bool $real
*/
public function setReal($real) {
$this->real = (bool) $real;
}
/**
* 设置模板文件的扩展名
* @param string $ext 扩展名
*/
public function setExtName($ext) {
$this->tExtName = $ext;
}
/**
* 临时文件目录
* @param string $dir
*/
public function setTmpDir($dir) {
if (!file_exists($dir)) {
if (!mkdir($dir, 0, true))
die("tmp dir $dir can't to mkdir");
}
$this->tTmpDir = realpath($dir);
}
/**
* URL调度器
* @param Dispatcher $dispatcher
*/
public function setU(&$dispatcher) {
if (is_object($dispatcher) && method_exists($dispatcher, 'U')) {
$this->uDispatcher = $dispatcher;
}
}
/**
* 注册用户自己的函数
* 当$function是string时则是方函数,如果是键值对数组时则键是类名,值是类中的静态方法
* @param mix $function 函数
*/
public function registerFunction($function) {
if (is_array($function)) {
foreach ($function as $key => $value) {
$this->userFunctions['classes'][$key] = $value;
}
} else {
$this->userFunctions['functions'][] = $function;
}
}
/**
* 变量赋值
* 如果$name是一个键值对的数组,则直接使用对$name数组进行赋值
* @param mixed $name 变量名
* @param mixed $value 值
*/
public function assign($name, $value=null) {
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->tVal[$key] = $val;
}
} else {
$this->tVal[$name] = $value;
}
}
/**
* 取得模版的变量
* @param string $name
*/
public function getVal($name) {
if (isset($this->tVal[$name])) {
return $this->tVal[$name];
}else
return false;
}
/**
* 将运行好后的内容,保存到一个html文件中
* @param string $tFile
* @param string $html
*/
public function saveHtml($tFile, $html) {
ob_start();
$this->display($tFile);
$buffer = ob_get_contents();
ob_end_clean();
file_put_contents($html, $buffer);
}
/**
* 运行并显示模版内容
* @param string $tfile
*/
public function display($tFile) {
$this->tFile = $this->parseTemplatePath($tFile);
if (!file_exists($this->getTmpFile()) || $this->real) {
$this->parse();
}
extract($this->tVal, EXTR_OVERWRITE);
include $this->getTmpFile();
}
/**
* 编译好后的文件
* @return string $filepath
*/
private function getTmpFile() {
$basename = basename($this->tFile);
$pos = strrpos($basename, '.');
$tmp = 'tpl_' . substr($basename, 0, $pos) . '.php';
return $this->tTmpDir . '/' . $tmp;
}
private function parse() {
$this->tContent = file_get_contents($this->tFile);
$this->parseInclude();
$this->parseSection();
$this->parseVal();
$this->parseEval();
if (!$this->real) {
//如果是在非调试环境下,则替换一些没用的内容
$search = array("/\r?\n/", "/\s{2,}/");
$repace = array('', '');
$this->tContent = preg_replace($search, $repace, $this->tContent);
}
file_put_contents($this->getTmpFile(), $this->tContent);
}
/**
* 解析模板中的子模板
*/
private function parseInclude() {
for ($i = 0; $i < 6; $i++) {
$this->tContent = preg_replace("/\{template\s+([a-zA-z0-9\._]+)\}/ies", "\$this->subtemplate('$1')", $this->tContent);
}
}
/**
* 获取只模版
* @param string $file
*/
private function subtemplate($file) {
return file_get_contents($this->parseTemplatePath($file));
}
/**
* 解析模版路径
* @param string $file
* @return string $filepath
*/
private function parseTemplatePath($tFile) {
$tFile.=$this->tExtName;
$tFile = $this->tDir ? $this->tDir . '/' . $tFile : $tFile;
if (!file_exists($tFile)) {
die("No template file $tFile");
} else {
$tFile = realpath($tFile);
}
return $tFile;
}
/**
* 解析变量
*/
private function parseVal() {
$this->tContent = preg_replace("/\{(\\$\S+?)\}/is", "", $this->tContent);
}
/**
* 解析段落
*/
private function parseSection() {
//逻辑
$this->tContent = preg_replace("/\{elseif\s+(.+?)\}/ies", "\$this->stripvtags('','')", $this->tContent);
$this->tContent = preg_replace("/\{else\}/is", "", $this->tContent);
$this->tContent = preg_replace("/\{U\((.+?)\)\}/ies", "\$this->parseUrl('$1')", $this->tContent);
//循环
for ($i = 0; $i < 6; $i++) {
$this->tContent = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies", "\$this->stripvtags('','\\3')", $this->tContent);
$this->tContent = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies", "\$this->stripvtags(' \\3) { ?>','\\4')", $this->tContent);
$this->tContent = preg_replace("/\{if\s+(.+?)\}(.+?)\{\/if\}/ies", "\$this->stripvtags('','\\2')", $this->tContent);
}
}
private function stripvtags($expr, $statement='') {
$expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
$statement = str_replace("\\\"", "\"", $statement);
return $expr . $statement;
}
/**
* 解析PHP语句
*/
private function parseEval() {
$this->tContent = preg_replace("/\{eval\s+(.+?)\}/is", "", $this->tContent);
}
/**
* 解析URL
*/
private function parseUrl($url) {
if (is_object($this->uDispatcher)) {
return $this->uDispatcher->U($url);
} else {
return $url;
}
}
}
?>
Magento是一套专业开源的PHP电子商务系统。Magento设计得非常灵活,具有模块化架构体系和丰富的功能。易于与第三方应用系统无缝集成。Magento开源网店系统的特点主要分以下几大类,网站管理促销和工具国际化支持SEO搜索引擎优化结账方式运输快递支付方式客户服务用户帐户目录管理目录浏览产品展示分析和报表Magento 1.6 主要包含以下新特性:•持久性购物 - 为不同的










