[Zope] non static size parameter in dtml-var

Steve Alexander steve@cat-box.net
Sun, 07 May 2000 22:14:24 +0100


Steve Alexander wrote:
> 
> Scott Parish wrote:
> >
> > I have a dtml-var tag that is being repeated over and over inside of a dtml-in
> > tag.  I would like the client to be able to custimize the maximum amount of
> > text output they see, so i did something like the following:
> >
> >  <dtml-var "item['title']" size="getTitleLen()">
> >
> > Where getTitleLen is a method which returns an integer of how many characters
> > max the title is to be.  However, when i have this dtml method rendered, i
> > get the following error:
> >
> >   a size attribute was used in a var tag with a non-integer value.
> 
> Looks like you'll need to patch lib/python/DocumentTemplate/DT_Var.py to
> get this working.
> 
> 326,328c326,331
> <             except: raise 'Document Error',(
> <                 '''a <code>size</code> attribute was used in a
> <code>var</code>
> <                 tag with a non-integer value.''')
> ---
> >             except:
> >             try: size=atoi(str(md[size]))
> >             except:
> >               raise 'Document Error',(
> >                   '''a <code>size</code> attribute was used in a <code>var</code>
> >                   tag with a non-integer value.''')
> 
> After this patch, you can use normal numeric literals, or you can use
> values from the namespace.

Except that the indentation of the patch hasn't come out right :-(

The final code should look like this:

        if have_arg('size'):
            size=args['size']
            try: size=atoi(size)
            except:
                try: size=atoi(str(md[size]))
                except:
                    raise 'Document Error',(
                          '''a <code>size</code> attribute
                          was used in a <code>var</code>
                          tag with a non-integer value.''')

Narrative:

If the literal value of the attribute "size" doesn't easily parse into
an integer, try looking up the value of the attribute "size" in the
namespace, and see if that wants to be a number. If all that fails, give
normal error message.

--
Steve Alexander
Software Engineer
Cat-Box limited