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

Arrays in smarty config file

 
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 -> Tips and Tricks
View previous topic :: View next topic  

Do you like it ?
Very much
0%
 0%  [ 0 ]
It is good
50%
 50%  [ 1 ]
Not necessary
50%
 50%  [ 1 ]
It is silly
0%
 0%  [ 0 ]
Total Votes : 2

Author Message
bunizz
Smarty Rookie


Joined: 11 Oct 2008
Posts: 8

PostPosted: Sun Dec 13, 2009 12:58 pm    Post subject: Arrays in smarty config file Reply with quote

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.
Back to top
View user's profile Send private message
Kekke
Smarty n00b


Joined: 17 Dec 2009
Posts: 4

PostPosted: Thu Dec 17, 2009 5:39 pm    Post subject: Reply with quote

Thank you
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 -> Tips and Tricks 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