Hmmm... so python does not like dashes within an identifier, making
this troublesome. You'll have to first off, get ride of the dashes in
"data-role", maybe just use underscores and rewrite them to be dashes
when rendering them in XML.

I think your best option is to create a new DIV class (or alter the
existing definition of class... meaning you can't upgrade web2py
easily).

Here's an example of a "mobile DIV" class that extends the standard
gluon class:

class mDIV(DIV):
    tag = 'div'


    def _xml(self):
        """
        helper for xml generation. Returns separately:
        - the component attributes
        - the generated xml of the inner components

        Component attributes start with an underscore ('_') and
        do not have a False or None value. The underscore is removed.
        A value of True is replaced with the attribute name.

        :returns: tuple: (attributes, components)
        """

        # get the attributes for this component
        # (they start with '_', others may have special meanings)
        fa = ''
        for key in sorted(self.attributes):
            value = self[key]
            if key[:1] != '_':
                continue
            name = key[1:]
            if value is True:
                value = name
            elif value is False or value is None:
                continue
############ MODIFICATIONS TO XML FUNCTION HERE ##############
            if name in ['data_role']: ### XXX - Add all other jQuery
mobile micro-data formats to this list - XXX
              name = name.replace("_",'-')
############ END MODS ##############################
            fa += ' %s="%s"' % (name, xmlescape(value, True))

        # get the xml for the inner components
        co = ''.join([xmlescape(component) for component in
                     self.components])

        return (fa, co)


Usage (note -- because I'm lazy I modified gluon/html.py, which is NOT
a good idea)

>>> from gluon.html import mDIV
>>> mDIV('test', _data_role='test').xml()
'<div data-role="test">test</div>'
>>>

You could probably throw that in modules and local_import it or in
models if it's used everywhere anyway. You would have to add the list
of attributes used by jQuery mobile (i'm really not knowledgable about
that).


This brings up an interesting issue, since the current TAG atributes
cannot contain dashes... this might be something that should be
patched, but we also need a sound policy on how to denote the dash (I
think converting non-leading underscores to dashes is fine, as I can't
think of any HTML element attributes that contain underscores...but I
could be terribly wrong.)

Just my $0.02


On Sep 27, 1:10 pm, Murray3 <cjjmur...@gmail.com> wrote:
> how do we do this with DIV() Helper?
>
> for instance in interactive i want to return a div element for
> collapsible elements
>
> >>>print gluon.DIV( data-role="collapsible" data-theme="contention")
>
> thanks

Reply via email to