Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
py:if statement newbie
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
ozwyzard  
View profile  
 More options Jun 7 2012, 12:45 am
From: ozwyzard <ozwyz...@gmail.com>
Date: Wed, 6 Jun 2012 21:45:20 -0700 (PDT)
Local: Thurs, Jun 7 2012 12:45 am
Subject: py:if statement newbie

Hello,

Is there a better way to construct the following without requiring two if
statements for each tab item?

Thanks!
----

<py:def function="sidebar(tabname)">
    <div class="sidebar-nav">
        <ul class="nav-list">
            <li py:if='tabname=="TabOne"' class="active"><a
href="#">TabOne</a></li>
            <li py:if='tabname!="TabOne"'><a href="#">TabOne</a></li>

            <li py:if='tabname=="TabTwo"' class="active"><a
href="#">TabTwo</a></li>
            <li py:if='tabname!="TabTwo"'><a href="#">TabTwo</a></li>
        </ul>
    </div>
</py:def>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jan Pokornư  
View profile  
 More options Jun 7 2012, 3:50 am
From: Jan Pokornư <jpoko...@redhat.com>
Date: Thu, 7 Jun 2012 09:50:14 +0200
Local: Thurs, Jun 7 2012 3:50 am
Subject: Re: py:if statement newbie
Hello,

On 06/06/12 21:45 -0700, ozwyzard wrote:

> Hello,

> Is there a better way to construct the following without requiring two if
> statements for each tab item?

Yes, it is.

> Thanks!
> ----

> <py:def function="sidebar(tabname)">
>     <div class="sidebar-nav">
>         <ul class="nav-list">
>             <li py:if='tabname=="TabOne"' class="active"><a href="#">TabOne</a></li>
>             <li py:if='tabname!="TabOne"'><a href="#">TabOne</a></li>
>             <li py:if='tabname=="TabTwo"' class="active"><a href="#">TabTwo</a></li>
>             <li py:if='tabname!="TabTwo"'><a href="#">TabTwo</a></li>
>         </ul>
>     </div>
> </py:def>

IIRC you can use something like:
  <li py:attrs='tabname=="TabOne" and {"class": "active"} or None'>
    <a href="#">TabOne</a>
  </li>

-- Jan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
fviktor  
View profile  
 More options Jun 7 2012, 4:57 am
From: fviktor <ferenczi.vik...@gmail.com>
Date: Thu, 7 Jun 2012 01:57:59 -0700 (PDT)
Local: Thurs, Jun 7 2012 4:57 am
Subject: Re: py:if statement newbie

No, since Genshi's XML template syntax does not provide any if-else nor
switch-case like construct.

This fact does not cause so many problems in practice, since the affected
code is usually small. For example if you build up your tabs from a list,
then only the <li> element would be doubled and only at a single place. The
resulting template is still valid XML and pretty much readable. So I can't
see the real issue here.

Please feel free to ask if you have a specific case where such doubling of
a few elements is discouraged.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Cross  
View profile  
 More options Jun 7 2012, 5:03 am
From: Simon Cross <hodges...@gmail.com>
Date: Thu, 7 Jun 2012 11:03:06 +0200
Local: Thurs, Jun 7 2012 5:03 am
Subject: Re: py:if statement newbie

On Thu, Jun 7, 2012 at 10:57 AM, fviktor <ferenczi.vik...@gmail.com> wrote:
> No, since Genshi's XML template syntax does not provide any if-else nor
> switch-case like construct.

It actually does provide a switch-like construct:
http://genshi.edgewall.org/wiki/Documentation/xml-templates.html#id2
-- such a construct just doesn't particularly help in this case.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
fviktor  
View profile  
 More options Jun 7 2012, 5:05 am
From: fviktor <ferenczi.vik...@gmail.com>
Date: Thu, 7 Jun 2012 02:05:58 -0700 (PDT)
Local: Thurs, Jun 7 2012 5:05 am
Subject: Re: py:if statement newbie

> IIRC you can use something like:
>   <li py:attrs='tabname=="TabOne" and {"class": "active"} or None'>
>     <a href="#">TabOne</a>
>   </li>

You're right on this in the strictly technical sense. I did not suggest
this solution in this case with a good reason. Using py:attrs in such cases
usually ended up in unreadable, hard to maintain code. According to my
experience it is much cleaner to just duplicate the affected element(s) if
the number of cases are low. I suggest using py:attrs only when the value
of the attribute is to be chosen from a larger set, for example when it is
an integer (rowspan, colspan) or a string (title, etc.).

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Boos  
View profile  
 More options Jun 7 2012, 5:06 am
From: Christian Boos <christian.b...@free.fr>
Date: Thu, 07 Jun 2012 11:06:21 +0200
Local: Thurs, Jun 7 2012 5:06 am
Subject: Re: py:if statement newbie
On 6/7/2012 6:45 AM, ozwyzard wrote:

Or not using <py:if> at all, but <py:when>:

<ul class="nav-list" py:choose="'TabTwo'">
   <py:when test="'TabOne'">
     <li class="active"><a href="#">TabOne</a></li>
     <li><a href="#">TabOne</a></li>
   </py:when>
   <py:when test="'TabTwo'">
     <li class="active"><a href="#">TabTwo</a></li>
     <li><a href="#">TabTwo</a></li>
   </py:when>
</ul>

Hope this helps!

-- Christian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
fviktor  
View profile  
 More options Jun 7 2012, 5:07 am
From: fviktor <ferenczi.vik...@gmail.com>
Date: Thu, 7 Jun 2012 02:07:20 -0700 (PDT)
Local: Thurs, Jun 7 2012 5:07 am
Subject: Re: py:if statement newbie

> It actually does provide a switch-like construct:
> http://genshi.edgewall.org/wiki/Documentation/xml-templates.html#id2
> -- such a construct just doesn't particularly help in this case.

You're right, I forgot about that construct, indeed. Maybe because I don't
really like it. :)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ozwyzard  
View profile  
 More options Jun 7 2012, 8:04 pm
From: ozwyzard <ozwyz...@gmail.com>
Date: Thu, 7 Jun 2012 17:04:23 -0700 (PDT)
Local: Thurs, Jun 7 2012 8:04 pm
Subject: Re: py:if statement newbie

Yes thanks.  The py:attrs statement you suggested works for me.  It works
without the 'or' clause as well.  

I looked at the following documentation, but could not come up with a
statement.
http://genshi.edgewall.org/wiki/Documentation/xml-templates.html#py-a...

I am aware of the general  (walkthrough) tutorial [
http://genshi.edgewall.org/wiki/Documentation ], but if you know of any
other tutorials or documentation that would have pointed me to this
particular syntax, please let me know.  

Thanks.
ozwyzard


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ozwyzard  
View profile  
 More options Jun 7 2012, 8:11 pm
From: ozwyzard <ozwyz...@gmail.com>
Date: Thu, 7 Jun 2012 17:11:18 -0700 (PDT)
Local: Thurs, Jun 7 2012 8:11 pm
Subject: Re: py:if statement newbie

Thank you all for the quick response(s).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Cross  
View profile  
 More options Jun 8 2012, 7:11 am
From: Simon Cross <hodges...@gmail.com>
Date: Fri, 8 Jun 2012 13:11:58 +0200
Local: Fri, Jun 8 2012 7:11 am
Subject: Re: py:if statement newbie

On Fri, Jun 8, 2012 at 2:04 AM, ozwyzard <ozwyz...@gmail.com> wrote:
> Yes thanks.  The py:attrs statement you suggested works for me.  It works
> without the 'or' clause as well.

This is a quirk of how Genshi is implemented (or maybe a documentation
bug :). py:attrs generates no attributes if it's value is something
that evaluates to False. I would probably have done 'or {}' for
clarity.

> I looked at the following documentation, but could not come up with a
> statement.
> http://genshi.edgewall.org/wiki/Documentation/xml-templates.html#py-a...

> I am aware of the general  (walkthrough) tutorial [
> http://genshi.edgewall.org/wiki/Documentation ], but if you know of any
> other tutorials or documentation that would have pointed me to this
> particular syntax, please let me know.

The syntax inside the py:attrs value is just standard Python.

See http://docs.python.org/reference/expressions.html#boolean-operations
for how the 'or' statement works.

Schiavo
Simon


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ozwyzard  
View profile  
 More options Jun 8 2012, 7:07 pm
From: ozwyzard <ozwyz...@gmail.com>
Date: Fri, 8 Jun 2012 16:07:11 -0700 (PDT)
Local: Fri, Jun 8 2012 7:07 pm
Subject: Re: py:if statement newbie

or {'active':None} as well.

http://genshi.edgewall.org/wiki/Documentation/xml-templates.html#py-a...

I read it to mean "foo" is a template-variable.  I could not find any doc
that suggests that "foo" could be in inline python statement that generates
a dictionary.  Glad it is though.

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »