Kid template: conditional attr

View: New views
5 Messages — Rating Filter:   Alert me  

Kid template: conditional attr

by gjhames :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello to everyone,
I'm using kid templates, and I have this tag:
<span py:content="myvar"></span>
But I want to put an CLASS to this SPAN, but the CLASS will be
depending on myvar, FOR EXAMPLE:

<span py:if="myvar=1" class="top"  py:content="myvar"></span>
<span py:if="myvar=2" class="bottom"  py:content="myvar"></span>
<span py:if="myvar=3" class="right"  py:content="myvar"></span>
<span py:if="myvar=456" class="left"  py:content="myvar"></span>


This works, but I'm wondering if there's a way to put all in one tag.
A conditional attr.

Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Kid template: conditional attr

by Christopher Arndt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


gjhames schrieb:

> Hello to everyone,
> I'm using kid templates, and I have this tag:
> <span py:content="myvar"></span>
> But I want to put an CLASS to this SPAN, but the CLASS will be
> depending on myvar, FOR EXAMPLE:
>
> <span py:if="myvar=1" class="top"  py:content="myvar"></span>
> <span py:if="myvar=2" class="bottom"  py:content="myvar"></span>
> <span py:if="myvar=3" class="right"  py:content="myvar"></span>
> <span py:if="myvar=456" class="left"  py:content="myvar"></span>
>
>
> This works, but I'm wondering if there's a way to put all in one tag.
> A conditional attr.

http://kid-templating.org/language.html#dynamic-attributes-py-attrs


Chris

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Kid template: conditional attr

by Christoph Zwerschke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Christopher Arndt schrieb:
> http://kid-templating.org/language.html#dynamic-attributes-py-attrs

Or instead of py:attrs you can also use
http://kid-templating.org/language.html#python-expression-substitution-expr

<span class="${{1:'top', 2:...}.get(myvar)}" py:content="..."/>

-- Christoph

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Kid template: conditional attr

by Petr Jakes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> <span class="${{1:'top', 2:...}.get(myvar)}" py:content="..."/>

hmmm... when trying:
<span class="${{1:'top', 2:'right'}.get(myvar)}" py:content="myvar"/>

I have got:
........
  File "/home/petr/TurboGears/virtuos/virtuos/templates/katalog.py",
line 136
    current = Element(u'{http://www.w3.org/1999/xhtml}span',
template_util.make_attrib({u'class': [{1:'top', 2:'right',
u'.get(myvar)}']}, self._get_assume_encoding()))
 
^
SyntaxError: invalid syntax

(sorry for the bad fromating, the ^ was under the ] bracket)

Petr Jakes
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Kid template: conditional attr

by Diez B. Roggisch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Petr Jakes schrieb:

>> <span class="${{1:'top', 2:...}.get(myvar)}" py:content="..."/>
>
> hmmm... when trying:
> <span class="${{1:'top', 2:'right'}.get(myvar)}" py:content="myvar"/>
>
> I have got:
> ........
>   File "/home/petr/TurboGears/virtuos/virtuos/templates/katalog.py",
> line 136
>     current = Element(u'{http://www.w3.org/1999/xhtml}span',
> template_util.make_attrib({u'class': [{1:'top', 2:'right',
> u'.get(myvar)}']}, self._get_assume_encoding()))
>  
> ^
> SyntaxError: invalid syntax
>
> (sorry for the bad fromating, the ^ was under the ] bracket)

That's because the first } closes the whole expression.

I'm not sure if there is any escaping proper for this, but what at least
works is to create a small local function for this.

<?python

def class_lookup(myvar):
     return {1 : "top", 2 : "right"}.get(myvar)
?>
<span class="${class_lookup(myvar)}"/>

Diez

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbogears@...
To unsubscribe from this group, send email to turbogears+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price!