(基于V2.6.20) |
1,Smarty.class.php |
//构造函数 |
function Smarty() |
{ |
$this->assign('SCRIPT_NAME', ...); |
} |
//assign函数 |
function assign($tpl_var, $value = null) |
{ |
if (is_array($tpl_var)){ //可以直接传递数组 |
foreach ($tpl_var as $key => $val) { |
if ($key != '') { |
$this->_tpl_vars[$key] = $val; |
} |
} |
} else { |
if ($tpl_var != '') |
$this->_tpl_vars[$tpl_var] = $value; |
} |
} |
重要:$this->_tpl_vars是保存模板变量的 |
//display方法 |
function display($resource_name, $cache_id = null, $compile_id = null) |
{ |
$this->fetch($resource_name, $cache_id, $compile_id, true); |
} |
//这个方法的逻辑还没有完全梳理清楚 |
function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) |
{ |
//debug.... |
//cache.... |
//调用方法_compile_resource来编译 |
} |
// |
function _compile_resource($resource_name, $compile_path) |
{ |
//调用方法_compile_source |
} |
/** |
* 编译给出的模板文件 |
* |
* @param string $resource_name |
* @param string $source_content |
* @param string $compiled_content |
* @return boolean |
*/ |
function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path=null) |
{ |
if (file_exists(SMARTY_DIR . $this->compiler_file)) { |
require_once(SMARTY_DIR . $this->compiler_file); |
} else { |
// use include_path |
require_once($this->compiler_file); |
} |
//实例化编译对象 |
$smarty_compiler = new $this->compiler_class; //Smarty_Compiler |
//初始化编译对象(传递相关的变量,包括上面的模板变量) |
$smarty_compiler->template_dir = $this->template_dir; |
$smarty_compiler->compile_dir = $this->compile_dir; |
$smarty_compiler->plugins_dir = $this->plugins_dir; |
$smarty_compiler->config_dir = $this->config_dir; |
$smarty_compiler->force_compile = $this->force_compile; |
$smarty_compiler->caching = $this->caching; |
$smarty_compiler->php_handling = $this->php_handling; |
$smarty_compiler->left_delimiter = $this->left_delimiter; |
$smarty_compiler->right_delimiter = $this->right_delimiter; |
$smarty_compiler->_version = $this->_version; |
$smarty_compiler->security = $this->security; |
$smarty_compiler->secure_dir = $this->secure_dir; |
$smarty_compiler->security_settings = $this->security_settings; |
$smarty_compiler->trusted_dir = $this->trusted_dir; |
$smarty_compiler->use_sub_dirs = $this->use_sub_dirs; |
$smarty_compiler->_reg_objects = &$this->_reg_objects; |
$smarty_compiler->_plugins = &$this->_plugins; |
$smarty_compiler->_tpl_vars = &$this->_tpl_vars; //模板变量 |
$smarty_compiler->default_modifiers = $this->default_modifiers; |
$smarty_compiler->compile_id = $this->_compile_id; |
$smarty_compiler->_config = $this->_config; |
$smarty_compiler->request_use_auto_globals = $this->request_use_auto_globals; |
if (isset($cache_include_path) && isset($this->_cache_serials[$cache_include_path])) { |
$smarty_compiler->_cache_serial = $this->_cache_serials[$cache_include_path]; |
} |
$smarty_compiler->_cache_include = $cache_include_path; |
//编译 |
$_results = $smarty_compiler->_compile_file($resource_name, $source_content, $compiled_content); |
if ($smarty_compiler->_cache_serial) { |
$this->_cache_include_info = array( |
'cache_serial'=>$smarty_compiler->_cache_serial |
,'plugins_code'=>$smarty_compiler->_plugins_code |
,'include_file_path' => $cache_include_path); |
} else { |
$this->_cache_include_info = null; |
} |
return $_results; |
} |
2,Smarty_Compiler.class.php |
//构造函数 |
function Smarty_Compiler(){ |
//初始化各种匹配用的正则表达式,例如匹配函数的:$this->_func_regexp = '[a-zA-Z_]\w*'; |
} |
//编译,使用$compiled_content返回编译好的php代码 |
/* |
返回的代码包括两部分: |
1,头部,如下: |
<?php //Smarty version 2.6.20, created on 2009-12-21 10:38:57 compiled from index.tpl |
?> |
<?php |
require_once(SMARTY_CORE_DIR . 'core.load_plugins.php'); |
//加载自定义插件,如load_data |
smarty_core_load_plugins(array('plugins' => array(array('function', 'load_data', 'index.tpl', 20, false),)), $this); ?> |
<?php $_smarty_tpl_vars = $this->_tpl_vars; //模板变量赋值 |
//加载头部模板 |
$this->_smarty_include(array('smarty_include_tpl_file' => 'header.tpl', 'smarty_include_vars' => array())); |
$this->_tpl_vars = $_smarty_tpl_vars; |
unset($_smarty_tpl_vars); |
?> |
2,模板主体 |
*/ |
function _compile_file($resource_name, $source_content, &$compiled_content){ |
//处理文本块 |
//编译所有标签,调用_compile_tag方法(重要) |
} |
注: |
1,literal标签内的数据smarty不会去分析(可用于显示有可能包含大括号等字符信息的 javascript脚本) |
2,可以使用php标签来嵌套php代码 |
参考:http://code.google.com/p/cyy0523xc/source/browse/trunk/php/Smarty%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B.txt