Discussion:
py:if statement newbie
ozwyzard
2012-06-07 04:45:20 UTC
Permalink
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 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 ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Jan Pokorný
2012-06-07 07:50:14 UTC
Permalink
Hello,
Post by ozwyzard
Hello,
Is there a better way to construct the following without requiring two if
statements for each tab item?
Yes, it is.
Post by ozwyzard
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
Post by ozwyzard
--
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.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
fviktor
2012-06-07 09:05:58 UTC
Permalink
Post by Jan Pokorný
<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 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/-/CJpEzZNVoMMJ.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
fviktor
2012-06-07 08:57:59 UTC
Permalink
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 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/-/13v9Qjvmb7EJ.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Simon Cross
2012-06-07 09:03:06 UTC
Permalink
Post by fviktor
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 received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
fviktor
2012-06-07 09:07:20 UTC
Permalink
Post by Simon Cross
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 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/-/dTlEwL3KbSsJ.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
ozwyzard
2012-06-08 00:11:18 UTC
Permalink
Thank you all for the quick response(s).
--
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/-/r-Fjti18RfwJ.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Christian Boos
2012-06-07 09:06:21 UTC
Permalink
Post by ozwyzard
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>
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 received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
ozwyzard
2012-06-08 00:04:23 UTC
Permalink
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
Post by ozwyzard
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 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/-/kcb5kNZrtrwJ.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Simon Cross
2012-06-08 11:11:58 UTC
Permalink
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
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
ozwyzard
2012-06-08 23:07:11 UTC
Permalink
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.
Post by Simon Cross
Post by ozwyzard
Yes thanks. The py:attrs statement you suggested works for me. It
works
Post by ozwyzard
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.
Post by ozwyzard
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
Post by ozwyzard
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 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/-/DhArGmbZZUEJ.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Loading...