DataGrid data - html formatting

3 views
Skip to first unread message

mte

unread,
Apr 16, 2006, 8:28:35 AM4/16/06
to TurboGears
Hello,
let's say I would like to have something like this:

MyClass:
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = '<span class="xy">%s</span>' % param2

and then somewhere in the code:
dg = DataGrid(fields=[('Param 1', 'param1'), ('Param 2', 'param2')])
data = [MyClass('abc', 'def'), MyClass('ghi', 'jkl')]

and the kid template:
py:replace="grid.display(data)"

This way, I get the '<span ...' displayed as text in html. The first
thing that came to my mind was using
py:replace="XML(grid.display(data))" but apparently it's not meant to
be used like this and it doesn't work.

So what I wrote on top is obviously a bad way to do it. What would be a
good way?
Thanks,
Matej

ajones

unread,
Apr 16, 2006, 5:22:22 PM4/16/06
to TurboGears
Instead of replacing an element from inside kid, call the code directly
from the element you want to contain the data. Example:

Before:
<div>
<span py:replace="grid.display(data)"/>
</div>

After:
<div>
{$grid.display(data)}
</div>

I'm pretty sure this avoids the escaping that kid normally does for you.

Alberto Valverde

unread,
Apr 16, 2006, 5:43:29 PM4/16/06
to turbo...@googlegroups.com

On 16/04/2006, at 14:28, mte wrote:

>
> Hello,
> let's say I would like to have something like this:
>
> MyClass:
> def __init__(self, param1, param2):
> self.param1 = param1
> self.param2 = '<span class="xy">%s</span>' % param2

Try:

self.param2 = XML('<span class="xy">%s</span>' % param2)

However, I'm not very sure that will work... this post is probably
random noise... :/

Alberto

bloode...@gmail.com

unread,
Apr 17, 2006, 7:51:37 AM4/17/06
to TurboGears
Alberto:

> Try:
> self.param2 = XML('<span class="xy">%s</span>' % param2)

As you say, not sure if this would work, I think XML has to be called
with a kid context.

I made this work for me in two ways:

1st attempt: I altered the datagrid.kid template to check for the
presence of an xml option and used XML() if so. Ugly but it worked.

IIRC it was somthing like :

<td py:for="c in columns"
py:content="c.get_option('xml', None) and XML(c.get_field(row)) or
c.get_field(row)">

and

dg = DataGrid(fields=[('Param1', 'param1'),
DataGrid.Column('Param2', 'param2', options=dict(xml=True))])

But then I though of a better way to do it : use a widget!

Again, from memory:

def widget_getter(widget, field):
def getter(row)
return widget.display(field=getattr(row, field, ''))
return getter

class Param2Widget(Widget):
template="""<span class="xy">$field</span>"""
template_vars = ['field']

dg = DataGrid(fields=[('Param1', 'param1'),
DataGrid.Column('Param2', widget_getter(Param2Widget(),
'param2'))])

Which may be a little overkill for your simple example, but it allows
you to use arbitarily complex html within each field (eg DataGrids
within DataGrids!). I mainly wanted to use it for things like putting
links in the table cells, or displaying join column info in sum manner
(e.g. as a list (with links!))

It should be possibly to just pass a widget its self as the getter arg
to DataGrid.Column, as it is callable, but I have not got round to
having a go at that yet.

HTH

--
wavydavy

mte

unread,
Apr 17, 2006, 2:56:37 PM4/17/06
to TurboGears
Hi,
Thank you all for replies.

> But then I though of a better way to do it : use a widget!
>

Thanks, this seems to be the cleanest way so far, I would have never
reminded of this!

>
> Which may be a little overkill for your simple example,

Not at all, I just always try to simplify things when posting
questions.

thanks,
matej

Reply all
Reply to author
Forward
0 new messages