Using a variable inside another variable name
Rémi ALVADO <remi.alvado <at> gmail.com>
2011-09-19 17:04:58 GMT
Hi,
I'm new with Cheetah and I'm trying to use it to build a deployment tool and replace my old configuration files with brand new cheetah templates.
I'm working for a company which edit websites all over the world so lots of our configuration keys are overriden by country.
With Cheetah, I'm storing my configuration into a YAML data storage files and I use a Python script combined with some Bash scripts to find, fill and replace all *.cheetah files.
Please find below an example of my test configuration file, my YAML data storage and the expected result file
--------------- CONF FILE -------------------
prod:
sso:
hosts:
#for myLanguage in $cultures
sso_$myLanguage:
host: $eval("sso_host_" + myLanguage)
port: $eval("sso_port_" + myLanguage)
#end for
--------------- YAML DATA STORAGE -------------------
# Global
cultures:
- de
- en
- fr
# DE
sso_port_de: 8081
[...]
sso_port_en: 8082
[...]
sso_port_fr: 8083
[...]
--------------- YAML DATA STORAGE -------------------
prod:
sso:
hosts:
sso_de:
port: 8081
sso_en:
port: 8082
sso_fr:
port: 8083
-------------------------------------
I know using "eval" is not a good idea (eval = evil :) ) but I've tried several other solutions ("$sso_public_host_$myLanguage", "$(sso_public_host_$myLanguage)", ...) and none of them work.
I've also tried to change my data storage file to have something like :
de:
sso_host_public: ...
but I've got the exact same issue with $self[$myLanguage]['sso_host_public'] and I don't like this representation since it's less natural than a list of <key,value> tuples.
Do you have any idea how I can use a variable inside another variable name ? It seems to be a bit odd but it would be very convenient in a production environnement to have a very simple data storage file with only <key,value> tuples : easy to read and easy to share with a simple copy/paste into release note on a mail, a twiki, ...
Thanks and congratulations this templating engine : despite this little issue, it's really great and I might consider using it on another projects, more web oriented :)
Rémi
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry® mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry® DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1
_______________________________________________
Cheetahtemplate-discuss mailing list
Cheetahtemplate-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss