The wt.rowaccess
module provides a
TemplateCode
sub-class which
provides an automatic way of creating replacements from values in a
dictionary.
This class is very simple to use. Simply ensure that the attribute named
by rowaccess_attrib
contains a dictionary with the keys and values you wish to have available as
attributes. This can, for example, be done in
main
.
The module works by defining a __getattr__
method that
consults the named dictionary, so the dictionary keys can be used as
attributes of the instance.
Example:
<!--wt:item-->
<a href="item.html?id=$$%ID$$">$$title$$</a>
<!--wt:/item-->
import jon.wt.rowaccess as rowaccess
class item(rowaccess.RowAccess):
def main(self, template):
website.dbc.execute("SELECT ID,title FROM items")
for self.row in dbc.fetchall():
self.process(template)
(Please note this example assumes your database connector can provide results as dictionaries.)
rowaccess_attrib
This variable may be overridden by subclasses. It is a string naming the
attribute which contains the dictionary to be consulted to find replacements.
The default value is "row"
.
$Id: wt-rowaccess.html,v 0416d65875b7 2014/03/05 17:37:06 jon $