On 10/7/07, Josh Rosenbaum <[EMAIL PROTECTED]> wrote:
> Paul Makepeace wrote:
> > I'm wondering if there's been any discussion on augmenting TT with a
> > pythonic (and many others) IN keyword. Allowing something awesomely
> > compact and readable like,
> >
> > <input type="checkbox" name="[% name %]" value="[% val %]"
> > [% IF val IN c.req.param(name); 'checked="checked"'; END %] />
> >
> > I'm at a loss how to achieve that without building up a hash. Any tips?
> >
> > P
>
> Ok, I'm assuming c.req.param(name) is returning a string with \0's in it.

(Out of curiosity, why would you assume that? That doesn't strike me
as something one would typically have.)

I should've been explicit: The IN keyword tests whether a scalar value
appears in an array. E.g.

  [% IF 'a' IN ['a', 'b'] %]
    Yes
  [% END %]

Inspired by e.g. python's,

  if 'a' in ('a', 'b'):
   print 'Yes'

> If it's a list then a simple grep would work.
>
> Not pretty and not very efficient, but this might work:
> [% IF c.req.param(name).split('\0').grep("^$val\$").size() %]
>
> or maybe something like:
> [% IF c.req.param(name).match("(^|\0)$val(\0|\$)") %]
>
> Note that the formatting or regular expressions could be off on those. I just 
> threw them out as an idea of what you might do so they may need refinement.
>
> You might just want to create your own vmethod or routine to do this, though. 
> For example something like api.is_checked(name, value) or maybe 
> c.req.param(name).is_checked(value).

Thanks for the ideas. The grep seems decent.

All these solutions certainly suggest IN could be useful ;)

P

>
> -- Josh
>

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to