HTML Tag generation

10 Aufrufe
Direkt zur ersten ungelesenen Nachricht

voltron

ungelesen,
10.12.2007, 05:59:5710.12.07
an Gluon Web Framework
Hi Massimo,

is there going to be some documentation on the usage/API of the HTML
generation soon? Examples of what I wuld like to know are:

1. Can one pass lasses to the tags?
2. The attributes that the tags take
3. Can the tags take default values?
4. I18n for the tags, how could this be realized

e.t.c

Thanks

Massimo Di Pierro

ungelesen,
10.12.2007, 09:36:1410.12.07
an gl...@googlegroups.com
> 1. Can one pass lasses to the tags?

I do not understand the question. What are lasses?

> 2. The attributes that the tags take

The tag objects are transparent to attributes that start by _, for
example:

>>> DIV('hello',H1('world'),_bla=5).xml()
<div bla="5">hello<h1>world</h1></div>

The only two non _ attributes are "requires" for input fields and
textares and "value". Value works like "_value" for input but plays
the role of selected for SELECT/OPTION and act as content for a
TEXTAREA. Examples

>>> SELECT('a','b','c',_name='test',value='b').xml()
<select name="test">
<option value='a'>a</option>
<option value='b' selected>b</option>
<option value='c'>c</option>
</select>
>>> TEXTAREA(_name='x',value="Hello World").ml()
<textarea name="x">HelloWorld</textarea>

> 3. Can the tags take default values?

You can create your own tag with

class myDIV(DIV):
default_attributes={'_style':'color: red;'}
def __init__(self,*components,**attributes):
DIV.__init__(self,*components,**attributes)
import copy
self.attributes=copy.copy(self.default_attributes)
for k,v in attributes.items(): self.attributes[k]=v

>> myDIV('hello',H1('world'),_id='test').xml()
<div id="test" style="color: red;">hello<h1>world</h1></div>

> 4. I18n for the tags, how could this be realized

Gluon doed not use the i18n module, has its own internationalization.
You can do this :

>>> H1(T("welcome")).xml()
<h1>ciao</h1>

(assuming Accept-Language is 'it' and 'it.py' says that "welcome" is
"ciao" in Italian).


voltron

ungelesen,
10.12.2007, 09:43:5710.12.07
an Gluon Web Framework
Sorry, thats was meant to be "CSS classes"

Massimo Di Pierro

ungelesen,
10.12.2007, 09:48:0810.12.07
an gl...@googlegroups.com
yes

>>> DIV('test',_class='myclass').xml()
<div class="myclass">test</div>

(this is why you have to use _ )
you can also do _onload, _onclick, etc. for ajax effects but you have
to use some ajax library.

michaelangela

ungelesen,
29.03.2008, 19:08:0729.03.08
an Massimo Di Pierro, web2py Web Framework
Seeing the new TAG construct, I was trying to make nested tags. Is
there something cleaner than this?

For example

In [81]: item=range(10)

In [82]: TAG.items(XML(''.join([TAG.item(i,_id=i).xml() for i in
item]))).xml()
Out[82]: '<items><item>0</item><item id="1">1</item><item id="2">2</
item><item id="3">3</item><item id="4">4</item><item id="5">5</
item><item id="6">6</item><item id="7">7</item><item id="8">8</
item><item id="9">9</item></items>'

Oh and for some reason '0' didn't create an ID here:

In [67]: [TAG.item(i,_id=i).xml() for i in item]
Out[67]:
['<item>0</item>',
'<item id="1">1</item>',
'<item id="2">2</item>',
'<item id="3">3</item>',
'<item id="4">4</item>',
'<item id="5">5</item>',
'<item id="6">6</item>',
'<item id="7">7</item>',
'<item id="8">8</item>',
'<item id="9">9</item>']

if I use item=range(1,10) it works as expected

In [97]: item=range(1,10)

In [98]: [TAG.item(i,_id=i).xml() for i in item]
Out[98]:
['<item id="1">1</item>',
'<item id="2">2</item>',
'<item id="3">3</item>',
'<item id="4">4</item>',
'<item id="5">5</item>',
'<item id="6">6</item>',
'<item id="7">7</item>',
'<item id="8">8</item>',
'<item id="9">9</item>']

I'll try to look into that a bit more. Any thoughts on nested
generation though? Or is there a way to "append" items to an existing
TAG item?

Michael

On Dec 10 2007, 7:48 am, Massimo Di Pierro <mdipie...@cs.depaul.edu>
wrote:
> yes
>
> >>> DIV('test',_class='myclass').xml()
> <div class="myclass">test</div>
>
> (this is why you have to use _ )
> you can also do _onload, _onclick, etc. for ajax effects but you have
> to use some ajax library.
>
> On Dec 10, 2007, at 8:43 AM, voltron wrote:
>
>
>
> > Sorry, thats was meant to be "CSS classes"
>
> > On Dec 10, 3:36 pm, Massimo Di Pierro <mdipie...@cs.depaul.edu> wrote:
> >>> 1. Can one pass lasses to the tags?
>
> >> I do not understand the question. What are lasses?
>
> >>> 2. The attributes that the tags take
>
> >> Thetagobjects are transparent to attributes that start by _, for

mdipierro

ungelesen,
29.03.2008, 19:53:4829.03.08
an web2py Web Framework
It is a bug. For now just do _id=str(i)

Michael Wills

ungelesen,
29.03.2008, 20:53:4629.03.08
an web...@googlegroups.com
Ah ok thanks. That works a treat. As for nesting tags then, is this a recommended way to do it?

In [81]: item=range(10)
In [82]: TAG.items(XML(''.join([TAG.item(i,_id=str(i)).xml() for i in item]))).xml()

Massimo Di Pierro

ungelesen,
29.03.2008, 21:13:3329.03.08
an web...@googlegroups.com
fixed in trunk 143

Massimo Di Pierro

ungelesen,
29.03.2008, 21:17:1729.03.08
an web...@googlegroups.com
I suggest you don't to that but

In [81]: item=range(10)
In [82]: print str(TAG.items(*[TAG.item(i,_id=str(i)) for i in item]))

It is shorter and cleaner. Note the star which unfolds the list into different arguments.
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten