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

Re: [Cheetahtemplate-discuss] help on loop variables

Expand Messages
  • James Abbatiello
    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 your object
    Message 1 of 5 , Jul 1, 2011
    • 0 Attachment
      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 your 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

      --
      James Abbatiello


      On Fri, Jul 1, 2011 at 4:05 PM, Tim Arnold <jtim.arnold@...> 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="$here.nodeName" xml:id="${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="${here.title.id}" class="center tabular"
      cellspacing="0" cellpadding="1" remap="$here.nodeName">
      <caption>${here.title}</caption>
      #else
      <informaltable class="tabular" remap="$here.nodeName">
      #end if
      #for $row in $here
       <tr>
       #for $cell in $row
         #if $cell.getVar('isHeader', False)
           <th style="$cell.style.inline">
            ${cell}</th>
         #else
           <td style="$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="self/title">
          <table tal:attributes="xml:id self/title/id" class="tabular"
      remap="tabular">
          <caption tal:content="self/title"></caption>
          <tr tal:repeat="row self">
          <metal:block tal:repeat="cell row">
            <th tal:condition="cell/isHeader"
                tal:attributes="style cell/style/inline"
                tal:content="cell">
            </th>
            <td tal:condition="not:cell/isHeader"
                tal:attributes="style cell/style/inline"
                tal:content="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 <abbeyj@...> 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 <jtim.arnold@...> 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 <abbeyj@...>
      >> 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 <jtim.arnold@...>
      >> > 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="$cell.style.inline">${cell}</th>
      >> >>
      >> >> Second, here is a similar template, but written in SimpleTal that does
      >> >> work:
      >> >> <metal:block tal:repeat="cell row">
      >> >>    <th tal:condition="cell/isHeader"
      >> >>           tal:attributes="style cell/style/inline;
      >> >>           tal:content="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’t 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
      >> >> Cheetahtemplate-discuss@...
      >> >> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      >> >
      >> >
      >
      >

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