smarty 給include file 加擴展

 

最近再寫一個東西,需要不同的用戶加載不同的模板,這樣就有一個問題,如果這個用戶的某些東西,沒有生成靜態緩存,或者我們後期給用戶添加一個塊的靜態緩存怎麼辦? 難道再不斷的去判斷這個文件是否存在不存在,怎麼樣,建立一個空的,等等,這樣如果後期修改了很多的話,那這個初始化的程序就會特別的大,而且不好維護,我們既然使用了smarty作爲我們的表現層,那我們擴展下表現層,來讓smarty支持,我們的這個方法,如果加載的文件不存在那麼加載一個默認的文件就可以了,這樣就算我們文件不存在也沒關係,當文件存在的時候自然就加載那個存在的文件去了。

關於smarty的 那個indeclude file有這樣的一種用法 來自手冊

 

{* windows absolute filepath (MUST use "file:" prefix) *}
{
include file="file:C:/www/pub/templates/header.tpl"}

{
* include from template resource named "db" *}
{
include file="db:header.tpl"}

那麼好吧,我們只需要擴展一個我們自己的文件類型就可以了

我們叫load,也就是說,如果文件不存在我們就自動加載一個我們默認的文件

再使用上我們將這樣使用

 

{% include file="load:commont_formaaa.tpl" %}

 那麼好我們現在看下如何擴展smarty

1,首先找到Smarty.class.php

然後繼續

2,我們會修改 function _parse_resource_name(&$params) 這個函數,基本再 1621行左右

我們修改成

 

function _parse_resource_name(&$params)
    {
        
// 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 [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章