py:if statement newbie

552 views
Skip to first unread message

ozwyzard

unread,
Jun 7, 2012, 12:45:20 AM6/7/12
to gen...@googlegroups.com
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>

Jan Pokorný

unread,
Jun 7, 2012, 3:50:14 AM6/7/12
to gen...@googlegroups.com
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 received this message because you are subscribed to the Google Groups "Genshi" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/genshi/-/VFa6Nv4XZH4J.
> To post to this group, send email to gen...@googlegroups.com.
> To unsubscribe from this group, send email to genshi+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
>

fviktor

unread,
Jun 7, 2012, 4:57:59 AM6/7/12
to gen...@googlegroups.com
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.

Simon Cross

unread,
Jun 7, 2012, 5:03:06 AM6/7/12
to gen...@googlegroups.com
On Thu, Jun 7, 2012 at 10:57 AM, fviktor <ferencz...@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.

fviktor

unread,
Jun 7, 2012, 5:05:58 AM6/7/12
to gen...@googlegroups.com

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.).

Christian Boos

unread,
Jun 7, 2012, 5:06:21 AM6/7/12
to gen...@googlegroups.com
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

fviktor

unread,
Jun 7, 2012, 5:07:20 AM6/7/12
to gen...@googlegroups.com
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. :) 

ozwyzard

unread,
Jun 7, 2012, 8:04:23 PM6/7/12
to gen...@googlegroups.com
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-attrs

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

ozwyzard

unread,
Jun 7, 2012, 8:11:18 PM6/7/12
to gen...@googlegroups.com
Thank you all for the quick response(s).

Simon Cross

unread,
Jun 8, 2012, 7:11:58 AM6/8/12
to gen...@googlegroups.com
On Fri, Jun 8, 2012 at 2:04 AM, ozwyzard <ozwy...@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-attrs
>
> 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

ozwyzard

unread,
Jun 8, 2012, 7:07:11 PM6/8/12
to gen...@googlegroups.com
or {'active':None} as well.

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

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.

On Friday, June 8, 2012 4:11:58 AM UTC-7, Hodgestar wrote:
Reply all
Reply to author
Forward
0 new messages