Discussion:
how to use py:attrs to add additional attributes?
Leho Kraav
2014-09-14 23:13:51 UTC
Permalink
hi nicholas, hi kindy!
do work indeed. however, after wondering for a bit why the accesskey
attribute still didn't show up, it turned out that it somehow gets
filtered: adding an attribute "foo" works fine, but as soon as i name
it "accesskey" it gets dropped again. i have no idea why that is, but
suppose it's specific to trac...
How would one add classes to an existing list based on the above examples?
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genshi+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/genshi.
For more options, visit https://groups.google.com/d/optout.
Simon Cross
2014-09-20 09:57:31 UTC
Permalink
Post by Leho Kraav
How would one add classes to an existing list based on the above examples?
This is a bit separate to the question asked in this thread, but a
possible solution is something like:

<a py:attrs="{'class': ['foo', 'bar'] + extra_classes}">...</a>

Schiavo
Simon
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genshi+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/genshi.
For more options, visit https://groups.google.com/d/optout.
Leho Kraav
2014-09-21 16:51:39 UTC
Permalink
Post by Leho Kraav
Post by Leho Kraav
How would one add classes to an existing list based on the above
examples?
This is a bit separate to the question asked in this thread, but a
<a py:attrs="{'class': ['foo', 'bar'] + extra_classes}">...</a>
I'm looking for something like merging the attribute values.

http://stackoverflow.com/a/17604869/35946

class="ticket" would want to become class="ticket foo bar"

This feels like the dict(list(list(select('@*'))[0]) + [('foo', 'p')])
magic would need to become some kind of a macro. It's not feasible to
repeat that conversion 20 times in a match template. Can this be done with
py:def?
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genshi+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/genshi.
For more options, visit https://groups.google.com/d/optout.
Simon Cross
2014-09-21 17:46:06 UTC
Permalink
Ah, so you're wanting to do this inside a py:match?

You might be able to make this work with py:def. Because Genshi
attempts to give guarantees on the correctness of the XML produced,
py:def produces a single block of XML/HTML. This may either be exactly
what you want or not. :)

If py:def doesn't work, you can always add a suitable helper to your
template context or define it in a code block [1].

[1] http://genshi.edgewall.org/wiki/Documentation/templates.html#id1

Aside: As a general rule, I would prefer using py:def over py:match
where possible. py:def is explicit and easy to understand. Lots of
py:match rules can make templates slow to render and hard to
understand.

Schiavo
Simon
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genshi+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/genshi.
For more options, visit https://groups.google.com/d/optout.
Leho Kraav
2014-09-21 17:32:30 UTC
Permalink
Post by Leho Kraav
Post by Leho Kraav
How would one add classes to an existing list based on the above
examples?
This is a bit separate to the question asked in this thread, but a
<a py:attrs="{'class': ['foo', 'bar'] + extra_classes}">...</a>
I'm looking for something like merging the attribute values.
http://stackoverflow.com/a/17604869/35946
class="ticket" would want to become class="ticket foo bar"
magic would need to become some kind of a macro. It's not feasible to
repeat that conversion 20 times in a match template. Can this be done
with py:def?
This is what I came up with:

<!-- early in the template -->
<?python
# https://groups.google.com/forum/#!topic/genshi/BS-KypqIHVQ
# http://stackoverflow.com/a/17604869/35946
def merge_attrs(attrs, moar_attrs):
if not isinstance(moar_attrs, dict) or not moar_attrs:
return attrs

attrs = dict(list(list(attrs)[0]))
keys = attrs.viewkeys() | moar_attrs.viewkeys()

return dict((k, attrs.get(k,"") + " " + moar_attrs.get(k,"")) for
k in keys)

# Exception: Unhandled node type <class '_ast.DictComp'>
#return {k: attrs.get(k,"") + " " + moar_attrs.get(k,"") for k
in keys}
?>

<!-- later -->

<div py:match="div[@id='content']" py:attrs="merge_attrs(select('@*'),
{'class': 'foo bar baz'})">${select("*")}</div>

Result:

<div class="ticket foo bar baz" id="content">


Looks good to me. Except I'd like to generalize this to be usable and
maintainable in a more centralized way, not just copy-paste into each
template everywhere. Could this be a core Genshi function of some sort?
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genshi+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/genshi.
For more options, visit https://groups.google.com/d/optout.
Simon Cross
2014-09-21 17:51:14 UTC
Permalink
Post by Leho Kraav
Looks good to me. Except I'd like to generalize this to be usable and
maintainable in a more centralized way, not just copy-paste into each
template everywhere. Could this be a core Genshi function of some sort?
I'm not sure that this should be part of core Genshi. I can imagine a
module of utilities and passing the module into your template context.
E.g.

"""utils.py"""

def merge_attrs(attrs, moar_attrs):
....

def other_things(...):
....

And then .generate(utils=utils).

I'm not completely closed to suggestions to add such a utility module
to Genshi itself (or incorporate it into Genshi in some other way),
but I think for now it would make more sense to develop the utilities
outside of Genshi and then incorporate them once we have a set that
works nicely?

Schiavo
Simon
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genshi+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/genshi.
For more options, visit https://groups.google.com/d/optout.
Loading...