Re: help on loop variables
James Abbatiello <[email protected]> Sat, 2 Jul 2011 00:49:32 -0400
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <CANhYFp1FPF9WH52zCS2z9TiteZUwL4T71xO3FFjOQOrT8fR7yg@mail.gmail.com> |
--===============2387149608430649992== Content-Type: multipart/alternative; boundary=20cf30563d2d2cfcf004a70edce3 --20cf30563d2d2cfcf004a70edce3 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Ah, I thought your object was like a dictionary and you were looking for a key in it. But I think what you want to do is look for an attribute on you= r object that may or may not be there. In that case try #if getattr($cell, 'isHeader', False) or #if hasattr($cell, 'isHeader') and $cell.isHeader --=20 James Abbatiello On Fri, Jul 1, 2011 at 4:05 PM, Tim Arnold <[email protected]> wrote: > Thanks James. By the way, I'm using Cheetah 2.4.4 and Python 2.7. > > If it worked for you then it must be the interface I've got for > Cheetah. For that reason I really can't give you a working example; I > don't want to waste your time, but in any case, here's the situation. > > I'm using the plasTeX Python package to convert LaTeX source to > DocBook XML and I'm using that package's mechanism to access Cheetah. > With this method, I provide a file with a bunch of templates that > match different document elements, like this: > ---------------------------- > name:label > engine:cheetah > <anchor remap=3D"$here.nodeName" xml:id=3D"${here.id}"/> > ---------------------------- > I think plastex reads that into a data structure to be called later on > after it parses the document. > > The particular template I was writing about was the following one. I > have an instance of a table and I'm iterating over its rows and cells > within rows. The resulting XML will be either a table (if a title was > on the tabular) or an informaltable (no title was given): > ---------------------------- > name: tabular > engine:cheetah > #if $varExists('here.title') and $here.title > <table xml:id=3D"${here.title.id}" class=3D"center tabular" > cellspacing=3D"0" cellpadding=3D"1" remap=3D"$here.nodeName"> > <caption>${here.title}</caption> > #else > <informaltable class=3D"tabular" remap=3D"$here.nodeName"> > #end if > #for $row in $here > <tr> > #for $cell in $row > #if $cell.getVar('isHeader', False) > <th style=3D"$cell.style.inline"> > ${cell}</th> > #else > <td style=3D"$cell.style.inline"> > ${cell}</td> > #end if > #end for > </tr> > #end for > #if $varExists('here.title') and $here.title > </table> > #else > </informaltable> > #end if > ---------------------------- > The if statement with .getVar is always false, but using simpletal > with identical logic I get the desired behavior. Here's what the > simpletal looks like (just showing the case of a tabular with a > title): > ---------------------------- > name: tabular > type: xml > <metal:block tal:condition=3D"self/title"> > <table tal:attributes=3D"xml:id self/title/id" class=3D"tabular" > remap=3D"tabular"> > <caption tal:content=3D"self/title"></caption> > <tr tal:repeat=3D"row self"> > <metal:block tal:repeat=3D"cell row"> > <th tal:condition=3D"cell/isHeader" > tal:attributes=3D"style cell/style/inline" > tal:content=3D"cell"> > </th> > <td tal:condition=3D"not:cell/isHeader" > tal:attributes=3D"style cell/style/inline" > tal:content=3D"cell"> > </td> > </metal:block> > </tr> > </table> > </metal:block> > > Thanks for reading this far :-) > > It may just be that I have too many levels of indirection; I assumed > it was a problem with Cheetah itself, but if it worked for you it must > be somehow between me, plastex, and Cheetah. > > thanks anyway, > --Tim > > > On Thu, Jun 30, 2011 at 7:21 PM, James Abbatiello <[email protected]> > wrote: > > Odd, it worked for me in my tests. What is the type of $cell in your > > template? Can you provide a minimal complete example? > > > > -- > > James Abbatiello > > > > > > On Thu, Jun 30, 2011 at 3:55 PM, Tim Arnold <[email protected]> > wrote: > >> > >> thanks for the help, but that doesn't work either. (not even getVar). > >> I think it's impossible to do this with Cheetah. > >> I'm rewriting this particular template in simpletal. > >> > >> thanks, > >> --Tim > >> On Tue, Jun 28, 2011 at 9:55 PM, James Abbatiello <[email protected]> > >> wrote: > >> > Try > >> > > >> > #if $cell.get('isHeader', False) > >> > > >> > or > >> > > >> > #if $cell.has_key('isHeader') and $cell.isHeader > >> > > >> > -- > >> > James Abbatiello > >> > > >> > > >> > On Tue, Jun 28, 2011 at 2:53 PM, Tim Arnold <[email protected]> > >> > wrote: > >> >> > >> >> As far as I can tell, it is impossible to use $varExists() inside a > >> >> loop variable. So I'm asking for help on how to re-think my > situation. > >> >> > >> >> First, here's what will not work in Cheetah. varExists is always > false: > >> >> #for $row in $here > >> >> <tr> > >> >> #for $cell in $row > >> >> #if $varExists('cell.isHeader') and $cell.isHeader > >> >> <th style=3D"$cell.style.inline">${cell}</th> > >> >> > >> >> Second, here is a similar template, but written in SimpleTal that > does > >> >> work: > >> >> <metal:block tal:repeat=3D"cell row"> > >> >> <th tal:condition=3D"cell/isHeader" > >> >> tal:attributes=3D"style cell/style/inline; > >> >> tal:content=3D"cell"></th> > >> >> </metal:block> > >> >> > >> >> I have some table cells with a varying suite of attributes; where > >> >> those attributes exist I want to use them, and of course I don=92t = want > >> >> to get an error when trying to use a non-existent attribute. > >> >> > >> >> Finally, my question: > >> >> is there any way to accomplish this with a Cheetah template? > >> >> > >> >> thanks, > >> >> --Tim Arnold > >> >> > >> >> > >> >> > >> >> > -------------------------------------------------------------------------= ----- > >> >> All of the data generated in your IT infrastructure is seriously > >> >> valuable. > >> >> Why? It contains a definitive record of application performance, > >> >> security > >> >> threats, fraudulent activity, and more. Splunk takes this data and > >> >> makes > >> >> sense of it. IT sense. And common sense. > >> >> http://p.sf.net/sfu/splunk-d2d-c2 > >> >> _______________________________________________ > >> >> Cheetahtemplate-discuss mailing list > >> >> [email protected] > >> >> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discus= s > >> > > >> > > > > > > --20cf30563d2d2cfcf004a70edce3 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Ah, I thought your object was like a dictionary and you were looking for a = key in it.=A0 But I think what you want to do is look for an attribute on y= our object that may or may not be there.=A0 In that case try<br><br>#if get= attr($cell, 'isHeader', False)<br> <br>or<br><br>#if hasattr($cell, 'isHeader') and $cell.isHeader<br>= <br>-- <br>James Abbatiello<br><br><br><div class=3D"gmail_quote">On Fri, J= ul 1, 2011 at 4:05 PM, Tim Arnold <span dir=3D"ltr"><<a href=3D"mailto:j= [email protected]">[email protected]</a>></span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">Thanks James. By the way, I'm using Che= etah 2.4.4 and Python 2.7.<br> <br> If it worked for you then it must be the interface I've got for<br> Cheetah. For that reason I really can't give you a working example; I<b= r> don't want to waste your time, but in any case, here's the situatio= n.<br> <br> I'm using the plasTeX Python package to convert LaTeX source to<br> DocBook XML and I'm using that package's mechanism to access Cheeta= h.<br> With this method, I provide a file with a bunch of templates that<br> match different document elements, like this:<br> ----------------------------<br> name:label<br> engine:cheetah<br> <anchor remap=3D"$here.nodeName" xml:id=3D"${<a href=3D"h= ttp://here.id" target=3D"_blank">here.id</a>}"/><br> ----------------------------<br> I think plastex reads that into a data structure to be called later on<br> after it parses the document.<br> <br> The particular template I was writing about was the following one. I<br> have an instance of a table and I'm iterating over its rows and cells<b= r> within rows. The resulting XML will be either a table (if a title was<br> on the tabular) or an informaltable (no title was given):<br> ----------------------------<br> name: tabular<br> engine:cheetah<br> #if $varExists('here.title') and $here.title<br> <table xml:id=3D"${<a href=3D"http://here.title.id" target=3D"_blan= k">here.title.id</a>}" class=3D"center tabular"<br> cellspacing=3D"0" cellpadding=3D"1" remap=3D"$here= .nodeName"><br> <caption>${here.title}</caption><br> #else<br> <informaltable class=3D"tabular" remap=3D"$here.nodeName&= quot;><br> #end if<br> <div class=3D"im">#for $row in $here<br> =A0<tr><br> =A0#for $cell in $row<br> </div> =A0 =A0#if $cell.getVar('isHeader', False)<br> <div class=3D"im"> =A0 =A0 =A0<th style=3D"$cell.style.inline"= ><br> =A0 =A0 =A0 ${cell}</th><br> </div> =A0 =A0#else<br> =A0 =A0 =A0<td style=3D"$cell.style.inline"><br> =A0 =A0 =A0${cell}</td><br> =A0 =A0#end if<br> =A0#end for<br> =A0 </tr><br> #end for<br> #if $varExists('here.title') and $here.title<br> =A0 =A0 </table><br> #else<br> =A0 =A0 </informaltable><br> #end if<br> ----------------------------<br> The if statement with .getVar is always false, but using simpletal<br> with identical logic I get the desired behavior. Here's what the<br> simpletal looks like (just showing the case of a tabular with a<br> title):<br> ----------------------------<br> name: tabular<br> type: xml<br> =A0 <metal:block tal:condition=3D"self/title"><br> =A0 =A0 <table tal:attributes=3D"xml:id self/title/id" class= =3D"tabular"<br> remap=3D"tabular"><br> =A0 =A0 <caption tal:content=3D"self/title"></caption&g= t;<br> =A0 =A0 <tr tal:repeat=3D"row self"><br> <div class=3D"im"> =A0 =A0 <metal:block tal:repeat=3D"cell row"= ;><br> =A0 =A0 =A0 <th tal:condition=3D"cell/isHeader"<br> =A0 =A0 =A0 =A0 =A0 tal:attributes=3D"style cell/style/inline"<b= r> =A0 =A0 =A0 =A0 =A0 tal:content=3D"cell"><br> =A0 =A0 =A0 </th><br> </div> =A0 =A0 =A0 <td tal:condition=3D"not:cell/isHeader"<br> <div class=3D"im"> =A0 =A0 =A0 =A0 =A0 tal:attributes=3D"style cell/st= yle/inline"<br> =A0 =A0 =A0 =A0 =A0 tal:content=3D"cell"><br> </div> =A0 =A0 =A0 </td><br> =A0 =A0 </metal:block><br> =A0 =A0 </tr><br> =A0 =A0 </table><br> =A0 </metal:block><br> <br> Thanks for reading this far :-)<br> <br> It may just be that I have too many levels of indirection; I assumed<br> it was a problem with Cheetah itself, but if it worked for you it must<br> be somehow between me, plastex, and Cheetah.<br> <br> thanks anyway,<br> <font color=3D"#888888">--Tim<br> </font><div><div></div><div class=3D"h5"><br> <br> On Thu, Jun 30, 2011 at 7:21 PM, James Abbatiello <<a href=3D"mailto:abb= [email protected]">[email protected]</a>> wrote:<br> > Odd, it worked for me in my tests.=A0 What is the type of $cell in you= r<br> > template?=A0 Can you provide a minimal complete example?<br> ><br> > --<br> > James Abbatiello<br> ><br> ><br> > On Thu, Jun 30, 2011 at 3:55 PM, Tim Arnold <<a href=3D"mailto:jtim= [email protected]">[email protected]</a>> wrote:<br> >><br> >> thanks for the help, but that doesn't work either. (not even g= etVar).<br> >> I think it's impossible to do this with Cheetah.<br> >> I'm rewriting this particular template in simpletal.<br> >><br> >> thanks,<br> >> --Tim<br> >> On Tue, Jun 28, 2011 at 9:55 PM, James Abbatiello <<a href=3D"m= ailto:[email protected]">[email protected]</a>><br> >> wrote:<br> >> > Try<br> >> ><br> >> > #if $cell.get('isHeader', False)<br> >> ><br> >> > or<br> >> ><br> >> > #if $cell.has_key('isHeader') and $cell.isHeader<br> >> ><br> >> > --<br> >> > James Abbatiello<br> >> ><br> >> ><br> >> > On Tue, Jun 28, 2011 at 2:53 PM, Tim Arnold <<a href=3D"ma= ilto:[email protected]">[email protected]</a>><br> >> > wrote:<br> >> >><br> >> >> As far as I can tell, it is impossible to use $varExists(= ) inside a<br> >> >> loop variable. So I'm asking for help on how to re-th= ink my situation.<br> >> >><br> >> >> First, here's what will not work in Cheetah. varExist= s is always false:<br> >> >> #for $row in $here<br> >> >> =A0<tr><br> >> >> =A0#for $cell in $row<br> >> >> =A0 =A0#if $varExists('cell.isHeader') and $cell.= isHeader<br> >> >> =A0 =A0<th style=3D"$cell.style.inline">$= {cell}</th><br> >> >><br> >> >> Second, here is a similar template, but written in Simple= Tal that does<br> >> >> work:<br> >> >> <metal:block tal:repeat=3D"cell row"><br> >> >> =A0 =A0<th tal:condition=3D"cell/isHeader"<b= r> >> >> =A0 =A0 =A0 =A0 =A0 tal:attributes=3D"style cell/sty= le/inline;<br> >> >> =A0 =A0 =A0 =A0 =A0 tal:content=3D"cell"><= ;/th><br> >> >> </metal:block><br> >> >><br> >> >> I have some table cells with a varying suite of attribute= s; where<br> >> >> those attributes exist I want to use them, and of course = I don=92t want<br> >> >> to get an error when trying to use a non-existent attribu= te.<br> >> >><br> >> >> Finally, my question:<br> >> >> is there any way to accomplish this with a Cheetah templa= te?<br> >> >><br> >> >> thanks,<br> >> >> --Tim Arnold<br> >> >><br> >> >><br> >> >><br> >> >> ---------------------------------------------------------= ---------------------<br> >> >> All of the data generated in your IT infrastructure is se= riously<br> >> >> valuable.<br> >> >> Why? It contains a definitive record of application perfo= rmance,<br> >> >> security<br> >> >> threats, fraudulent activity, and more. Splunk takes this= data and<br> >> >> makes<br> >> >> sense of it. IT sense. And common sense.<br> >> >> <a href=3D"http://p.sf.net/sfu/splunk-d2d-c2" target=3D"_= blank">http://p.sf.net/sfu/splunk-d2d-c2</a><br> >> >> _______________________________________________<br> >> >> Cheetahtemplate-discuss mailing list<br> >> >> <a href=3D"mailto:[email protected]= ge.net">[email protected]</a><br> >> >> <a href=3D"https://lists.sourceforge.net/lists/listinfo/c= heetahtemplate-discuss" target=3D"_blank">https://lists.sourceforge.net/lis= ts/listinfo/cheetahtemplate-discuss</a><br> >> ><br> >> ><br> ><br> ><br> </div></div></blockquote></div><br> --20cf30563d2d2cfcf004a70edce3-- --===============2387149608430649992== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2 --===============2387149608430649992== 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 --===============2387149608430649992==--