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