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, &#39;isHeader&#39;, False)<br>
<br>or<br><br>#if hasattr($cell, &#39;isHeader&#39;) 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">&lt;<a href=3D"mailto:j=
[email protected]">[email protected]</a>&gt;</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&#39;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&#39;ve got for<br>
Cheetah. For that reason I really can&#39;t give you a working example; I<b=
r>
don&#39;t want to waste your time, but in any case, here&#39;s the situatio=
n.<br>
<br>
I&#39;m using the plasTeX Python package to convert LaTeX source to<br>
DocBook XML and I&#39;m using that package&#39;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>
&lt;anchor remap=3D&quot;$here.nodeName&quot; xml:id=3D&quot;${<a href=3D"h=
ttp://here.id" target=3D"_blank">here.id</a>}&quot;/&gt;<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&#39;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(&#39;here.title&#39;) and $here.title<br>
&lt;table xml:id=3D&quot;${<a href=3D"http://here.title.id" target=3D"_blan=
k">here.title.id</a>}&quot; class=3D&quot;center tabular&quot;<br>
cellspacing=3D&quot;0&quot; cellpadding=3D&quot;1&quot; remap=3D&quot;$here=
.nodeName&quot;&gt;<br>
&lt;caption&gt;${here.title}&lt;/caption&gt;<br>
#else<br>
&lt;informaltable class=3D&quot;tabular&quot; remap=3D&quot;$here.nodeName&=
quot;&gt;<br>
#end if<br>
<div class=3D"im">#for $row in $here<br>
 =A0&lt;tr&gt;<br>
 =A0#for $cell in $row<br>
</div> =A0 =A0#if $cell.getVar(&#39;isHeader&#39;, False)<br>
<div class=3D"im"> =A0 =A0 =A0&lt;th style=3D&quot;$cell.style.inline&quot;=
&gt;<br>
 =A0 =A0 =A0 ${cell}&lt;/th&gt;<br>
</div> =A0 =A0#else<br>
 =A0 =A0 =A0&lt;td style=3D&quot;$cell.style.inline&quot;&gt;<br>
 =A0 =A0 =A0${cell}&lt;/td&gt;<br>
 =A0 =A0#end if<br>
 =A0#end for<br>
 =A0 &lt;/tr&gt;<br>
#end for<br>
#if $varExists(&#39;here.title&#39;) and $here.title<br>
 =A0 =A0 &lt;/table&gt;<br>
#else<br>
 =A0 =A0 &lt;/informaltable&gt;<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&#39;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 &lt;metal:block tal:condition=3D&quot;self/title&quot;&gt;<br>
 =A0 =A0 &lt;table tal:attributes=3D&quot;xml:id self/title/id&quot; class=
=3D&quot;tabular&quot;<br>
remap=3D&quot;tabular&quot;&gt;<br>
 =A0 =A0 &lt;caption tal:content=3D&quot;self/title&quot;&gt;&lt;/caption&g=
t;<br>
 =A0 =A0 &lt;tr tal:repeat=3D&quot;row self&quot;&gt;<br>
<div class=3D"im"> =A0 =A0 &lt;metal:block tal:repeat=3D&quot;cell row&quot=
;&gt;<br>
 =A0 =A0 =A0 &lt;th tal:condition=3D&quot;cell/isHeader&quot;<br>
 =A0 =A0 =A0 =A0 =A0 tal:attributes=3D&quot;style cell/style/inline&quot;<b=
r>
 =A0 =A0 =A0 =A0 =A0 tal:content=3D&quot;cell&quot;&gt;<br>
 =A0 =A0 =A0 &lt;/th&gt;<br>
</div> =A0 =A0 =A0 &lt;td tal:condition=3D&quot;not:cell/isHeader&quot;<br>
<div class=3D"im"> =A0 =A0 =A0 =A0 =A0 tal:attributes=3D&quot;style cell/st=
yle/inline&quot;<br>
 =A0 =A0 =A0 =A0 =A0 tal:content=3D&quot;cell&quot;&gt;<br>
</div> =A0 =A0 =A0 &lt;/td&gt;<br>
 =A0 =A0 &lt;/metal:block&gt;<br>
 =A0 =A0 &lt;/tr&gt;<br>
 =A0 =A0 &lt;/table&gt;<br>
 =A0 &lt;/metal:block&gt;<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 &lt;<a href=3D"mailto:abb=
[email protected]">[email protected]</a>&gt; wrote:<br>
&gt; Odd, it worked for me in my tests.=A0 What is the type of $cell in you=
r<br>
&gt; template?=A0 Can you provide a minimal complete example?<br>
&gt;<br>
&gt; --<br>
&gt; James Abbatiello<br>
&gt;<br>
&gt;<br>
&gt; On Thu, Jun 30, 2011 at 3:55 PM, Tim Arnold &lt;<a href=3D"mailto:jtim=
[email protected]">[email protected]</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; thanks for the help, but that doesn&#39;t work either. (not even g=
etVar).<br>
&gt;&gt; I think it&#39;s impossible to do this with Cheetah.<br>
&gt;&gt; I&#39;m rewriting this particular template in simpletal.<br>
&gt;&gt;<br>
&gt;&gt; thanks,<br>
&gt;&gt; --Tim<br>
&gt;&gt; On Tue, Jun 28, 2011 at 9:55 PM, James Abbatiello &lt;<a href=3D"m=
ailto:[email protected]">[email protected]</a>&gt;<br>
&gt;&gt; wrote:<br>
&gt;&gt; &gt; Try<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; #if $cell.get(&#39;isHeader&#39;, False)<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; or<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; #if $cell.has_key(&#39;isHeader&#39;) and $cell.isHeader<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; --<br>
&gt;&gt; &gt; James Abbatiello<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Tue, Jun 28, 2011 at 2:53 PM, Tim Arnold &lt;<a href=3D"ma=
ilto:[email protected]">[email protected]</a>&gt;<br>
&gt;&gt; &gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; As far as I can tell, it is impossible to use $varExists(=
) inside a<br>
&gt;&gt; &gt;&gt; loop variable. So I&#39;m asking for help on how to re-th=
ink my situation.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; First, here&#39;s what will not work in Cheetah. varExist=
s is always false:<br>
&gt;&gt; &gt;&gt; #for $row in $here<br>
&gt;&gt; &gt;&gt; =A0&lt;tr&gt;<br>
&gt;&gt; &gt;&gt; =A0#for $cell in $row<br>
&gt;&gt; &gt;&gt; =A0 =A0#if $varExists(&#39;cell.isHeader&#39;) and $cell.=
isHeader<br>
&gt;&gt; &gt;&gt; =A0 =A0&lt;th style=3D&quot;$cell.style.inline&quot;&gt;$=
{cell}&lt;/th&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Second, here is a similar template, but written in Simple=
Tal that does<br>
&gt;&gt; &gt;&gt; work:<br>
&gt;&gt; &gt;&gt; &lt;metal:block tal:repeat=3D&quot;cell row&quot;&gt;<br>
&gt;&gt; &gt;&gt; =A0 =A0&lt;th tal:condition=3D&quot;cell/isHeader&quot;<b=
r>
&gt;&gt; &gt;&gt; =A0 =A0 =A0 =A0 =A0 tal:attributes=3D&quot;style cell/sty=
le/inline;<br>
&gt;&gt; &gt;&gt; =A0 =A0 =A0 =A0 =A0 tal:content=3D&quot;cell&quot;&gt;&lt=
;/th&gt;<br>
&gt;&gt; &gt;&gt; &lt;/metal:block&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I have some table cells with a varying suite of attribute=
s; where<br>
&gt;&gt; &gt;&gt; those attributes exist I want to use them, and of course =
I don=92t want<br>
&gt;&gt; &gt;&gt; to get an error when trying to use a non-existent attribu=
te.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Finally, my question:<br>
&gt;&gt; &gt;&gt; is there any way to accomplish this with a Cheetah templa=
te?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; thanks,<br>
&gt;&gt; &gt;&gt; --Tim Arnold<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; ---------------------------------------------------------=
---------------------<br>
&gt;&gt; &gt;&gt; All of the data generated in your IT infrastructure is se=
riously<br>
&gt;&gt; &gt;&gt; valuable.<br>
&gt;&gt; &gt;&gt; Why? It contains a definitive record of application perfo=
rmance,<br>
&gt;&gt; &gt;&gt; security<br>
&gt;&gt; &gt;&gt; threats, fraudulent activity, and more. Splunk takes this=
 data and<br>
&gt;&gt; &gt;&gt; makes<br>
&gt;&gt; &gt;&gt; sense of it. IT sense. And common sense.<br>
&gt;&gt; &gt;&gt; <a href=3D"http://p.sf.net/sfu/splunk-d2d-c2" target=3D"_=
blank">http://p.sf.net/sfu/splunk-d2d-c2</a><br>
&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt; &gt;&gt; Cheetahtemplate-discuss mailing list<br>
&gt;&gt; &gt;&gt; <a href=3D"mailto:[email protected]=
ge.net">[email protected]</a><br>
&gt;&gt; &gt;&gt; <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>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;<br>
&gt;<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==--