Hi,

On 7/9/11 8:10 PM, Stéphane Klein wrote:
> sometime, I've template like this :
> 
> <div class="item {% if obj.selected %}selected{% endif %}{% if
> obj.collapsed %}collapsed{% endif %}">...</div>
> 
> I think this is not clean.
> 
> Is there something like css_classes in Jinja2 ?
No, but I also think that is not necessary, but something similar could
prove useful:

  @environmentfilter
  def css_classes(env, classes):
      return u' '.join(unicode(x) for x in classes if x) \
             or env.undefined(hint='No classes requested')

  env.filters['css_classes'] = css_classes

And then you can use it like this:

Simple case:

  <td class="{{ ['foo', 'bar']|css_classes }}">

    --> <td class="foo bar">

Conditional case:

  <td class="{{ ['foo', 'bar' if true, 'baz' if false]|css_classes }}">

    --> <td class="foo bar">

With XML attributes:

  <td{{ dict(class=['foo', 'bar' if true, 'baz'
    if false]|css_classes)|xmlattr }}>

    --> <td class="foo bar">

With XML attributes and nothing left:

  <td{{ dict(class=['baz' if false]|css_classes)|xmlattr }}>

    --> <td>

Hope that filter is helpful.


Regards,
Armin

-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pocoo-libs?hl=en.

Reply via email to