Loading ...
Sorry, an error occurred while loading the content.
 

[Cheetahtemplate-discuss] Using a variable inside another variable name

Expand Messages
  • Rémi ALVADO
    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
    Message 1 of 2 , Sep 19, 2011
      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_host_de:         sso-de.example.com
      sso_port_de:         8081
      [...]

      # EN
      sso_host_en:         sso-en.example.com
      sso_port_en:         8082
      [...]

      # FR
      sso_host_fr:         sso-fr.example.com
      sso_port_fr:         8083
      [...]

      --------------- YAML DATA STORAGE -------------------
      prod:
        sso:
          hosts:
            sso_de:
              host:   sso-de.example.com
              port:   8081
            sso_en:
              host:   sso-en.example.com
              port:   8082
            sso_fr:
              host:   sso-fr.example.com
              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
    • Oliver Nicholas
      $getVar( sso_host_%s % $myLanguage) ... -- oliver phillip nicholas *computer whisperer* http://bigo.yelp.com/ $getVar( sso_host_%s % $myLanguage) On Mon, Sep
      Message 2 of 2 , Sep 19, 2011
        $getVar("sso_host_%s" % $myLanguage)

        On Mon, Sep 19, 2011 at 10:04 AM, Rémi ALVADO <remi.alvado@...> wrote:
        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_host_de:         sso-de.example.com
        sso_port_de:         8081
        [...]

        # EN
        sso_host_en:         sso-en.example.com
        sso_port_en:         8082
        [...]

        # FR
        sso_host_fr:         sso-fr.example.com
        sso_port_fr:         8083
        [...]

        --------------- YAML DATA STORAGE -------------------
        prod:
          sso:
            hosts:
              sso_de:
                host:   sso-de.example.com
                port:   8081
              sso_en:
                host:   sso-en.example.com
                port:   8082
              sso_fr:
                host:   sso-fr.example.com
                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&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
        Learn about the latest advances in developing for the
        BlackBerry&reg; mobile platform with sessions, labs & more.
        See new tools and technologies. Register for BlackBerry&reg; DevCon today!
        http://p.sf.net/sfu/rim-devcon-copy1
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss




        --
        oliver phillip nicholas
        computer whisperer
        http://bigo.yelp.com/

      Your message has been successfully submitted and would be delivered to recipients shortly.