bunizz Smarty Rookie
Joined: 11 Oct 2008 Posts: 8
|
Posted: Sun Dec 13, 2009 12:58 pm Post subject: Arrays in smarty config file |
|
|
There is a project on which I am working, and the project is required a multi-language system. While I was searching on the net, I came accross a discussion about the best way for multi-language in smarty. I read all of them, and I decided that using smarty config file for it is the best way.
There are some reasons of my decidion. First of all, putting every text stuff in a single config file makes the translation very very easy for translator, because the synax is very simple. I will just send the original conf file, en_EN.conf, to translator, (s)he translate it, and send it back to me.
Here is the example sytax, it is simple, isn't it :)
Anyway, I started to coding, and keep coding ... Then something was happened.
I need some variables in the config file as an array. I have to store Countries, Jobs, etc. in my language config file, ofcourse as an array.
If I use single variable instead of array, I have to define all of them in config file like this:
Code: |
country_AFG = "..."
...
country_FRA = "..."
...
country_RUS = "..."
...
country_TUR = " . .." |
And then, I have to use them in .tpl file like this: (by writing whole thing by hand)
Code: |
<option value="AFG">#country_AFG#</<option>
...
<option value="FRA">#country_FRA#</<option>
...
<option value="RUS">#country_RUS#</<option>
...
<option value="TUR">#country_TUR#</<option>
... |
While there is a perfect function which is {html_options}, it is silly to write whole <option ...> </option> part by hand.
Instead of writing whole <option>..</option> part above, we can just use {html_options} in a single line.
So, I started to find out a way to use variables as an array, and I have found, and I have written, and I am sharing now.
First of all, we define a function in php:
Code: |
function configVarToArray($options, &$smarty) {
// getting the config variable
$static =& $smarty->get_config_vars( $options['config_var'] );
// we are seperating it per line.
$rows = explode( "\n", $static);
$return=array();
foreach ( $rows as $line ) {
// if the line is empty, then skip it
if ( empty( $line ) )
continue;
// we are again seperating each line into two part.
// everything before [TAB] character is our key,
// and after it is our value.
$country = explode ( " ", $line);
$return[ $country[0] ] = trim( $country[1] );
}
// and now, we transfer our work to standart smarty variable.
$smarty->assign( $options['array_var'] , $return );
} |
Secondly we register the function,
Code: |
$smarty->register_function('makeConfigArray', 'configVarToArray'); |
Thirdly, we define a config varible in our langue_config_file:
(because the tab character can not be seen exactly, I will show them as [TAB], So you should change "[TAB]" into real TAB character while using :) )
Code: |
stable_var_1 = """0[TAB]Select one of them
1[TAB]stuff 1
2[TAB]Stuff 2
3[TAB]Stuff 3
4[TAB]Noting but stuff"""
|
And then, In the tpl file, we can use it like this:
Code: | {makeConfigArray config_var=stable_var_1 array_var=new_var}
{html_options name=trying options=$new_var} |
And finally, the output:
Code: | <select name="trying">
<option label="Stuff 1" value="1">Stuff 1</option>
<option label="Stuff 2" value="2">Stuff 2</option>
<option label="Stuff 3" value="3">Stuff 3</option>
<option label="Noting but stuff" value="4">Noting but stuff</option>
</select> |
That's all. I hope it will help you,
Thanks. |
|