Adding custom method to a custom widget?

6 views
Skip to first unread message

iain duncan

unread,
Jan 26, 2007, 7:19:52 PM1/26/07
to turbo...@googlegroups.com
I know how to get a widget to look stuff up itself by passing in a
reference to a function, allowing the widget template to call the
function. IE

class BlockHeader(widgets.Widget):
template = """
<div>Page title: ${dbf(page_id).title}</div>
"""

params = [ 'page_id', 'dbf' ]


and in the controller:

w_header = BlockHeader( page_id=1, dbf=databaseFunction )


My question is whether I could instead add a custom helper function to
the widget class definition and somehow have the template be able to
call this function. At the moment it uses eval to conduct some jiggery
pokery around the name ...

If I add a method to the widget the template does not have access to it.
Should I try to add a property to the widgets template object? Is there
someway to access the widgets methods from within the template object?

Christopher Arndt

unread,
Jan 26, 2007, 7:37:09 PM1/26/07
to turbo...@googlegroups.com
iain duncan schrieb:

> My question is whether I could instead add a custom helper function to
> the widget class definition and somehow have the template be able to
> call this function. [...]

>
> If I add a method to the widget the template does not have access to it.

Override the update_params method in the widget and add the function to the
params dictionary. This will give the template access to the function.

def foo():
return "Hello, World!"

class MyWidget(widgets.Widget):
template = """
<div xmlns:py="http://purl.org/kid/ns#" id="mywidget">
<p py:content="foo()" />
</div>"""

def update_params(self, params)
super(MyWidget, self).update_params(params)
params['foo'] = foo

HTH, Chris

iain duncan

unread,
Jan 26, 2007, 9:28:55 PM1/26/07
to turbo...@googlegroups.com

It did, thanks. Works like a charm!
Iain


Reply all
Reply to author
Forward
0 new messages