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

Re: [Cheetahtemplate-discuss] variables in template

Expand Messages
  • James Abbatiello
    On Fri, Jul 12, 2013 at 11:32 AM, Redhorse, Olivia J. (WSTF-RD)[Jacobs ... The ordinary way to do this is to create an array of N elements instead of N
    Message 1 of 2 , Jul 12, 2013
    • 0 Attachment

      On Fri, Jul 12, 2013 at 11:32 AM, Redhorse, Olivia J. (WSTF-RD)[Jacobs Technology Inc] <olivia.j.redhorse@...> wrote:
      #for $i in $hdrcol:
      #set $fldvalue = ‘$’ + ‘spform[spdata].sp’ + str( $i )
      <input type=”text” name=”testfield” value=”$fldvalue”/>
      #end for
       
      In the code above, the input value $fldvalue will display $spform[spdata].sp0 and not the actual value from spform dictionary.  I am trying to create an incrementing value such as; sp1, sp2, sp3,..etc in $fldvalue to retrieve that values from the dictionary into the input field.  It continues to display as a string and I cannot figure out how to display the value from dictionary.  How can I accomplish this?  I hope this makes sense. Thanks, Olivia

      The ordinary way to do this is to create an array of N elements instead of N separate fields.  That would look something like this:
          Foo = collections.namedtuple('Foo', ['sp'])
          foo = Foo(sp=['this is first', 'this is second', 'this is third'])
          namespace = {
              'hdrcol': [0, 1, 2],
              'spdata': 'foo',
              'spform': {
                  'foo': foo
              }
          }
          print Cheetah.Template.Template("""
      #for $i in $hdrcol:
      #set $fldvalue = $spform[$spdata].sp[$i]
      <input type="text" name="testfield" value="$fldvalue"/>
      #end for
      """, searchList=[namespace])

      If you are unable to change the format of the data and you're stuck with sp0, sp1, sp2, ... then you have to get a little more clever.  First construct the name of the field you want, then use getattr to get it:
          Foo = collections.namedtuple('Foo', ['sp0', 'sp1', 'sp2'])
          foo = Foo(sp0='this is first', sp1='this is second', sp2='this is third')
          namespace = {
              'hdrcol': [0, 1, 2],
              'spdata': 'foo',
              'spform': {
                  'foo': foo
              }
          }
          print Cheetah.Template.Template("""
      #for $i in $hdrcol:
      #set $fldvalue = getattr($spform[$spdata], 'sp' + str( $i ))
      <input type="text" name="testfield" value="$fldvalue"/>
      #end for
      """, searchList=[namespace])


      As an aside, you posted code with smart quotes (and in Comic Sans) and this makes it harder to help you since copying and pasting your example won't work as-is.  In the future you may want to try avoiding this it makes it less likely that you'll get a response.

      --
      James Abbatiello

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