最近再写一个东西,需要不同的用户加载不同的模板,这样就有一个问题,如果这个用户的某些东西,没有生成静态缓存,或者我们后期给用户添加一个块的静态缓存怎么办? 难道再不断的去判断这个文件是否存在不存在,怎么样,建立一个空的,等等,这样如果后期修改了很多的话,那这个初始化的程序就会特别的大,而且不好维护,我们既然使用了smarty作为我们的表现层,那我们扩展下表现层,来让smarty支持,我们的这个方法,如果加载的文件不存在那么加载一个默认的文件就可以了,这样就算我们文件不存在也没关系,当文件存在的时候自然就加载那个存在的文件去了。
关于smarty的 那个indeclude file有这样的一种用法 来自手册
{ include file = " file:C:/www/pub/templates/header.tpl " }
{ * include from template resource named " db " * }
{ include file = " db:header.tpl " }
那么好吧,我们只需要扩展一个我们自己的文件类型就可以了
我们叫load,也就是说,如果文件不存在我们就自动加载一个我们默认的文件
再使用上我们将这样使用
那么好我们现在看下如何扩展smarty
1,首先找到Smarty.class.php
然后继续
2,我们会修改 function _parse_resource_name(&$params) 这个函数,基本再 1621行左右
我们修改成
{
// split tpl_path by the first colon
$_resource_name_parts = explode ( ' : ' , $params [ ' resource_name ' ] , 2 );
if ( count ( $_resource_name_parts ) == 1 ) {
// no resource type given
$params [ ' resource_type ' ] = $this -> default_resource_type;
$params [ ' resource_name ' ] = $_resource_name_parts [ 0 ];
} else {
if ( strlen ( $_resource_name_parts [ 0 ]) == 1 ) {
// 1 char is not resource type, but part of filepath
$params [ ' resource_type ' ] = $this -> default_resource_type;
$params [ ' resource_name ' ] = $params [ ' resource_name ' ];
} else {
$params [ ' resource_type ' ] = $_resource_name_parts [ 0 ];
$params [ ' resource_name ' ] = $_resource_name_parts [ 1 ];
}
}
// sanshi edit start 2008-2-13 old /* if ($params['resource_type'] == 'file') */
if ( $params [ ' resource_type ' ] == ' file ' || $params [ ' resource_type ' ] == ' load ' ) {
// sanshi edit end 2008-2-13
if ( ! preg_match ( ' /^([///]|[a-zA-Z]:[///])/ ' , $params [ ' resource_name ' ])) {
// relative pathname to $params['resource_base_path']
// use the first directory where the file is found
foreach (( array ) $params [ ' resource_base_path ' ] as $_curr_path ) {
$_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params [ ' resource_name ' ];
if ( file_exists ( $_fullpath ) && is_file ( $_fullpath )) {
$params [ ' resource_name ' ] = $_fullpath ;
return true ;
}
// didn't find the file, try include_path
$_params = array ( ' file_path ' => $_fullpath );
require_once (SMARTY_CORE_DIR . ' core.get_include_path.php ' );
if (smarty_core_get_include_path( $_params , $this )) {
$params [ ' resource_name ' ] = $_params [ ' new_file_path ' ];
return true ;
} else {
// sanshi add start 2008-2-13
if ( $params [ ' resource_type ' ] == ' load ' )
{
$params [ ' resource_name ' ] = $params [ ' resource_base_path ' ] . " not_file.tpl " ;
return true ;
}
// sanshi add end 2008-2-13
}
}
return false ;
} else {
/* absolute path */
return file_exists ( $params [ ' resource_name ' ]);
}
} elseif ( empty ( $this -> _plugins[ ' resource ' ][ $params [ ' resource_type ' ]])) {
$_params = array ( ' type ' => $params [ ' resource_type ' ]);
require_once (SMARTY_CORE_DIR . ' core.load_resource_plugin.php ' );
smarty_core_load_resource_plugin( $_params , $this );
}
return true ;
}
其他的地方就不用修改了,就实现了,不存在就加载的功能,不过需要注意的是
$params['resource_base_path']."not_file.tpl"; 这个文件要存在,当然您可以修改成您自己的,我的这个是在 templates 目录下,有一个 not_file.tpl 这样的一个文件作为我的默认加载文件,这个扩展就算完事了。
作者 叁石 sanshi0815
mail sanshi0815@tom.com