Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Multiple config-file directories

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
rainco
Smarty Rookie


Joined: 13 Oct 2003
Posts: 31
Location: Germany

PostPosted: Tue Nov 18, 2003 12:56 am    Post subject: Multiple config-file directories Reply with quote

Hi!

I wrote a patch for using multiple config-file directories by adding the following lines to function.config_load.php:

Code:

} elseif (is_array($smarty->config_dir)) {                             // multiple config_dirs
    if (preg_match("/^(.*):(.*)$/", $_file, $matches)) {               // resource given
        $_config_res  = $matches[1];                                   // set config resource
        $_file = $matches[2];                                          // set config file
        if (@is_dir($smarty->config_dir[$_config_res])) {              // resource is valid
            $_config_dir = $smarty->config_dir[$_config_res];          // use custom resource
        } else {                                                       // resource is not valid
            $smarty->_syntax_error("config resource '".$_config_res."' is not valid",
                                   E_USER_ERROR, __FILE__, __LINE__);  // raise error
        }
    } else {                                                           // no resource given
        if (@is_dir($smarty->config_dir['default'])) {                 // default resource valid
            $_config_dir = $smarty->config_dir['default'];             // use default resource
        } else {                                                       // default resource not valid
            $smarty->_syntax_error("default config_dir is not valid",
                                   E_USER_ERROR, __FILE__, __LINE__);  // raise error
        }
    }


This way it is possibe to use them in manner very much like templates resources. It is 100% downward compatible, but if you define $smarty->config_dir as an array you can access config_files through
Code:

{config_load file="resource:file"}

or
Code:

$smarty->config_load("resource:file");


$smarty->config_dir than should look something like this:
Code:

$smarty->config_dir = array("default" => "configs", "resource" => "configs2", ...);


I would be very glad if this can find its way into the smarty distribution.

Thanks,
Rainer

PS: The patch follows.

Code:

57a58,75
>         } elseif (is_array($smarty->config_dir)) {                             // multiple config_dirs
>             if (preg_match("/^(.*):(.*)$/", $_file, $matches)) {               // resource given
>                 $_config_res  = $matches[1];                                   // set config resource
>                 $_file = $matches[2];                                          // set config file
>                 if (@is_dir($smarty->config_dir[$_config_res])) {              // resource is valid
>                     $_config_dir = $smarty->config_dir[$_config_res];          // use custom resource
>                 } else {                                                       // resource is not valid
>                     $smarty->_syntax_error("config resource '".$_config_res."' is not valid",
>                                            E_USER_ERROR, __FILE__, __LINE__);  // raise error
>                 }
>             } else {                                                           // no resource given
>                 if (@is_dir($smarty->config_dir['default'])) {                 // default resource valid
>                     $_config_dir = $smarty->config_dir['default'];             // use default resource
>                 } else {                                                       // default resource not valid
>                     $smarty->_syntax_error("default config_dir is not valid",
>                                            E_USER_ERROR, __FILE__, __LINE__);  // raise error
>                 }
>             }
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Nov 18, 2003 1:40 am    Post subject: Reply with quote

This is indeed nice!

Unfortunately, I must cast my vote as -1 to include this in the distribution -- I think a more general approach that is in-line with the template resource handler would be more appropriate.

In the meantime, this will likely help a few people out -- thanks, rainco!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP