Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Unable to change config dir

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
ChrisMTaopix
Smarty n00b


Joined: 13 Aug 2013
Posts: 3

PostPosted: Tue Aug 13, 2013 3:15 pm    Post subject: Unable to change config dir Reply with quote

Hello,

I am trying to load config files dependant on their availability.

Config file 1 is the default and will always be present.

Config file 2 and 3 might be present.

I set the config dir for config 1 and read the config settings in - no problems.

When I detect that config file 2 or 3 are present, I change the config directory and read the config in.
However I only ever have the settings form config file 1

Code:


$smarty = new Smarty();

$smarty->config_booleanize = false;
   
$smarty->setCompileDir('./compiled');
   
$smarty->setTemplateDir('./templates');

$smarty->config_overwrite = true;

$smarty->compile_id = 'config1';
$smarty->setConfigDir('./config1');

$smarty->configLoad('settings.conf');

$smarty->compile_id = 'config2';
$smarty->setConfigDir('./config2');

$smarty->configLoad('settings.conf');

$smarty->compile_id = 'config3';
$smarty->setConfigDir('./config3');

$smarty->configLoad('settings.conf');

$smarty->display('index.tpl');



Note: I have removed all code for checking that the files exist.

config1/settings.conf:

Code:

title = "config 1"


config2/settings.conf:

Code:

title = "config 2"


config3/settings.conf:

Code:

title = "config 3"


template:

Code:

<!DOCTYPE html>
<html lang="en_gb">
    <head>
        <meta charset="UTF-8" />
        <title>{#title#}</title>
    </head>
     <body>
        {#title#}<br>
     </body>
</html>


This will only ever output "config 1"

If I change

Code:
$smarty->configLoad('settings.conf');


to

Code:
$smarty->configLoad('./config2/settings.conf');


It works.

I have had a look at the compiled files and all the compiled settings.conf files and they contain the contents of config1/settings.conf.

I have put debug in the smarty code and I cannot see where it is reading the location of the file.

If I call getConfigDir after setting it, it gives me the directory I have just set.

Is this a bug or is there something I am doing wrong.

I have recently upgraded from Smarty2 and this did work.

Cheers

Chris
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Aug 13, 2013 9:03 pm    Post subject: Reply with quote

Indeed this was a bug. The fix is now in the SVN trunk and will later be included in 3.1.15

Some comments about your example:

1. In Smarty3 it's no longer necessary to use a compile_id for templates or config files of same name in different folders.

2. template_dir and config_dir can be an array of folders. Smarty will loop over the array until it will find a folder containing the requested template or config file.

Example
Code:
$smarty->setConfigDir(array('./config3','./config2','./config1'));
$smarty->configLoad('settings.conf');
Back to top
View user's profile Send private message
ChrisMTaopix
Smarty n00b


Joined: 13 Aug 2013
Posts: 3

PostPosted: Wed Aug 14, 2013 8:12 am    Post subject: Reply with quote

Excellent. Thank you for your answer.
I have patched our version of Smarty with the change in SVN and it works.
Very Happy

However if I don't set the compile_id it only ever reads from the first config file.
Looking at the buildCompiledFilepath function in smarty_internal_config.php I have noticed that it is using just the file name to create the filepath.

Code:
$_filepath = sha1($this->source->name . $_flag);


Changing the line to this works, because it is honoring the directory as well:

Code:
$_filepath = sha1($this->source->filepath . $_flag);


Will this cause any problems or should I go down the complie_id route?

I have just tried setting all my config directories in one hit as suggested:

Code:
   
$smarty->setConfigDir('./config1', './config2', './config3');
$smarty->configLoad('settings.conf');
$smarty->display('index.tpl');


This doesn't work for me because I want to merge all versions of settings.conf together if the file exists i.e.
If settings.conf in config2 has setting title and settings.conf in config1 doesn't, it will be added to the config vars array.
If there is a settings in config1 and config2, the config 2 setting overrides config1.

Will I have to implement this myself or is there already a way in Smarty to do this?[/code]
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Aug 14, 2013 7:42 pm    Post subject: Reply with quote

I missed the filepath issue yesterday because I had force_compile on while testing.

The SVN is now updated again.

If you want to merge config files from different folders the
$smarty->setConfigDir('./config1', './config2', './config3');
does not work.

For this you have to process the differen config_dir's one by one.
Back to top
View user's profile Send private message
ChrisMTaopix
Smarty n00b


Joined: 13 Aug 2013
Posts: 3

PostPosted: Thu Aug 15, 2013 7:01 am    Post subject: Reply with quote

Thanks for the reply.
I will carry on looping through the different config files then.
Can I submit a change request so that all config files of the same names are merged?

When is 3.1.15 set to be released?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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