[web2py] Re: jquery mobile and returning DIV() with content format

2011-09-27 Thread Murray3
thanks guys, i will  see if I can get my collapsible list generated
from a controller function using Anthony's idea.
On Sep 27, 8:39 pm, Anthony  wrote:
> On Tuesday, September 27, 2011 3:33:44 PM UTC-4, kasapo wrote:
>
> > Ok, Anthony's idea is like a million times better.
>
> I stole it from a previous post by Massimo. :-)


[web2py] Re: jquery mobile and returning DIV() with content format

2011-09-27 Thread Anthony
On Tuesday, September 27, 2011 3:33:44 PM UTC-4, kasapo wrote:
>
> Ok, Anthony's idea is like a million times better.
>

I stole it from a previous post by Massimo. :-) 


[web2py] Re: jquery mobile and returning DIV() with content format

2011-09-27 Thread kasapo
Ok, Anthony's idea is like a million times better.

-1 for extending the div class

:(

On Sep 27, 2:32 pm, kasapo  wrote:
> 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()
>
> 'test'
>
>
>
> 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  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


[web2py] Re: jquery mobile and returning DIV() with content format

2011-09-27 Thread kasapo
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()
'test'
>>>

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


[web2py] Re: jquery mobile and returning DIV() with content format

2011-09-27 Thread Anthony
You cannot do DIV(..., _data-role="collapsible") because _data-role is not a 
valid keyword argument in Python due to the hyphen. Instead, you could do:

DIV(..., **{'_data-role': 'collapsible'})

or

mydiv = DIV(...)
mydiv['_data-role'] = 'collapsible'

Anthony

On Tuesday, September 27, 2011 2:10:00 PM UTC-4, Murray3 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 
>
>