Re: variables in template
James Abbatiello <[email protected]> Fri, 12 Jul 2013 22:42:47 -0400
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <CANhYFp3jhdk5PsKcCe8sj73pUFknMr1Vrak-5WPJ3JqUxs+DFg@mail.gmail.com> |
--===============3564560716730417037== Content-Type: multipart/alternative; boundary=089e0160d05422eaf604e15b95de --089e0160d05422eaf604e15b95de Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Fri, Jul 12, 2013 at 11:32 AM, Redhorse, Olivia J. (WSTF-RD)[Jacobs Technology Inc] <[email protected]> wrote: > #for $i in $hdrcol: > #set $fldvalue =3D =91$=92 + =91spform[spdata].sp=92 + str( $i ) > <input type=3D=94text=94 name=3D=94testfield=94 value=3D=94$fldvalue=94/> > #end for > > In the code above, the input value $fldvalue will display > $spform[spdata].sp0 and not the actual value from spform dictionary. I a= m > 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 t= o > display the value from dictionary. How can I accomplish this? I hope th= is > 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 =3D collections.namedtuple('Foo', ['sp']) foo =3D Foo(sp=3D['this is first', 'this is second', 'this is third']) namespace =3D { 'hdrcol': [0, 1, 2], 'spdata': 'foo', 'spform': { 'foo': foo } } print Cheetah.Template.Template(""" #for $i in $hdrcol: #set $fldvalue =3D $spform[$spdata].sp[$i] <input type=3D"text" name=3D"testfield" value=3D"$fldvalue"/> #end for """, searchList=3D[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 =3D collections.namedtuple('Foo', ['sp0', 'sp1', 'sp2']) foo =3D Foo(sp0=3D'this is first', sp1=3D'this is second', sp2=3D'this = is third') namespace =3D { 'hdrcol': [0, 1, 2], 'spdata': 'foo', 'spform': { 'foo': foo } } print Cheetah.Template.Template(""" #for $i in $hdrcol: #set $fldvalue =3D getattr($spform[$spdata], 'sp' + str( $i )) <input type=3D"text" name=3D"testfield" value=3D"$fldvalue"/> #end for """, searchList=3D[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. --=20 James Abbatiello --089e0160d05422eaf604e15b95de Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><br><div class=3D"gmail_extra"><div class=3D"gmail_quote">= On Fri, Jul 12, 2013 at 11:32 AM, Redhorse, Olivia J. (WSTF-RD)[Jacobs Tech= nology Inc] <span dir=3D"ltr"><<a href=3D"mailto:olivia.j.redhorse@nasa.= gov" target=3D"_blank">[email protected]</a>></span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-= left:1px solid rgb(204,204,204);padding-left:1ex"> <div> <font face=3D"Comic Sans MS, cursive"> <div>#for $i in $hdrcol:</div> <div>#set $fldvalue =3D =91$=92 + =91spform[spdata].sp=92 + str( $i )</div> <div><input type=3D=94text=94 name=3D=94testfield=94 value=3D=94$fldvalu= e=94/></div> <div>#end for</div> <div>=A0</div> <div>In the code above, the input value $fldvalue will display $spform[spda= ta].sp0 and not the actual value from spform dictionary.=A0 I am trying to = create an incrementing value such as; sp1, sp2, sp3,..etc in $fldvalue to r= etrieve that values from the dictionary into the input field.=A0 It continues to display as a string and I cannot f= igure out how to display the value from dictionary.=A0 How can I accomplish= this?=A0 I hope this makes sense. Thanks, Olivia</div></font></div></block= quote> </div><br></div><div class=3D"gmail_extra">The ordinary way to do this is t= o create an array of N elements instead of N separate fields.=A0 That would= look something like this:<br><span style=3D"font-family:courier new,monosp= ace">=A0=A0=A0 Foo =3D collections.namedtuple('Foo', ['sp']= )<br> =A0=A0=A0 foo =3D Foo(sp=3D['this is first', 'this is second= 9;, 'this is third'])<br>=A0=A0=A0 namespace =3D {<br>=A0=A0=A0=A0= =A0=A0=A0 'hdrcol': [0, 1, 2],<br>=A0=A0=A0=A0=A0=A0=A0 'spdata= ': 'foo',<br>=A0=A0=A0=A0=A0=A0=A0 'spform': {<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 'foo': foo<br>=A0=A0=A0=A0=A0=A0= =A0 }<br>=A0=A0=A0 }<br>=A0=A0=A0 print Cheetah.Template.Template("&qu= ot;"<br>#for $i in $hdrcol:<br>#set $fldvalue =3D $spform[$spdata].sp[= $i]<br><input type=3D"text" name=3D"testfield" value= =3D"$fldvalue"/><br> #end for<br>""", searchList=3D[namespace])<br></span><br></d= iv><div class=3D"gmail_extra">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 l= ittle more clever.=A0 First construct the name of the field you want, then = use getattr to get it:<br> <span style=3D"font-family:courier new,monospace">=A0=A0=A0 Foo =3D collect= ions.namedtuple('Foo', ['sp0', 'sp1', 'sp2'= ])<br>=A0=A0=A0 foo =3D Foo(sp0=3D'this is first', sp1=3D'this = is second', sp2=3D'this is third')<br> =A0=A0=A0 namespace =3D {<br>=A0=A0=A0=A0=A0=A0=A0 'hdrcol': [0, 1,= 2],<br>=A0=A0=A0=A0=A0=A0=A0 'spdata': 'foo',<br>=A0=A0=A0= =A0=A0=A0=A0 'spform': {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 '= foo': foo<br>=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0 }<br>=A0=A0=A0 print = Cheetah.Template.Template("""<br> #for $i in $hdrcol:<br>#set $fldvalue =3D getattr($spform[$spdata], 'sp= ' + str( $i ))<br><input type=3D"text" name=3D"testfi= eld" value=3D"$fldvalue"/><br>#end for<br>""&qu= ot;, searchList=3D[namespace])<br> </span><br></div><div class=3D"gmail_extra"><br></div><div class=3D"gmail_e= xtra">As an aside, you posted code with smart quotes (and in Comic Sans) an= d this makes it harder to help you since copying and pasting your example w= on't work as-is.=A0 In the future you may want to try avoiding this it = makes it less likely that you'll get a response.<br> <br>-- <br></div><div class=3D"gmail_extra">James Abbatiello<br><br></div><= /div> --089e0160d05422eaf604e15b95de-- --===============3564560716730417037== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ See everything from the browser to the database with AppDynamics Get end-to-end visibility with application monitoring from AppDynamics Isolate bottlenecks and diagnose root cause in seconds. Start your free trial of AppDynamics Pro today! http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk --===============3564560716730417037== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Cheetahtemplate-discuss mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss --===============3564560716730417037==--