Python Webpages

Dave Cole djc at object-craft.com.au
Wed May 1 11:56:53 EDT 2002


>>>>> "Peter" == Peter Dobcsanyi <dpeter at designtheory.org> writes:

Peter> While we are at it, I would be interested in opinions on
Peter> Albatross:

Peter>     http://object-craft.com.au/projects/albatross/

I suppose I shouldn't really respond since I am slightly biased in my
views...

Albatross has a templating system which is a bit like DTML.  So to
compare with the jonpy example for output:

> Python already has several equivalents of this. I think you can do
> better though, which is what jonpy does. You instead do simply:
> 
>   $$title$$
> 
> and then in your Python code:
> 
>   class main(wt.TemplateCode):
>     def title(self):
>       return "Hello there!"

In Albatross:

    ctx.locals.title = 'Hello there!'

and in the template:

    <al-value expr="title">

> This more object-oriented approach is both simpler and more powerful,
> since you can use class inheritance to very easily make re-usable
> configurable objects. Have repeated sections such as table rows is
> also very easy using 'sections':
> 
>   <!--wt:newsitem-->
>     <tr><td><a href="newsitem.html?id=$$%ID$$">$$title$$</a></td></tr>
>   <!--wt:/newsitem-->
> 
>   class main(wt.TemplateCode):
>     class newsitem(rowaccess.RowAccess):
>       def main(self, template):
>         dbc = website.dbh.cursor()
>         dbc.execute("SELECT ID,title FROM newsitems")
>         for self.row in dbc.fetchall()
>           self.process(template)

This translates to the following template:

    <al-for iter="r" expr="newsitems">
      <tr><td><al-a expr="'newsitem.html?id=%s' % r.value().ID"><al-value expr="r.value().title"></a></td></tr>
    </al-for>

The al-for tag uses an iterator object (r in this case) to iterate
over a sequence.  Special care is taken to ensure that __getitem__()
is only called once for each element of the sequence so you can use
classes which just implement the sequence protocol (xreadlines() for
example).  The current element of the sequence is available via the
value() method of the iterator.

And the code:

    c = website.dbh.cursor()
    c.execute("SELECT ID,title FROM newsitems")
    ctx.locals.newsitems = c.fetchall()

The templating is a bit of a sideshow in some ways when you start
wondering how you are going to accept browser input back into the
complex structures that you just traversed to get your templates to
produce output.

Consider the case where you want to have a checkbox next to each news
item which appears in the output list.  In Albatross some of the work
is done for you.  The template code could look like this:

    <al-for iter="r" expr="newsitems">
      <tr><td>
        <al-input type="checkbox" nameexpr="'newsitems[%d].check' % r.index()">
        <al-a expr="'newsitem.html?id=%s' % r.value().ID">
        <al-value expr="r.value().title"></a>
      </td></tr>
    </al-for>

And then the code which prepares the newsitems could look like this:

    c = website.dbh.cursor()
    c.execute("SELECT ID,title,check=0 FROM newsitems")
    ctx.locals.newsitems = c.fetchall()
    ctx.add_session_vars('newsitems')

When the browser submitted a response the input would transparently
set the check attribute of the respective row object to 'on' if the
user checked that box during input.

If for any reason you displayed the same list of row objects again
(without fetching them from the database again), the value of the just
input check attribute would control the presence of the checked
attribute in the HTML output --- all without you writing a single line
of code.

That is quite a bit of tedious code you didn't have to write.

It gets more interesting for nested data such as trees...  Check out
this sample:

  http://www.object-craft.com.au/cgi-bin/alsamp/tree1/tree.py

The tree display is explained here:

  http://www.object-craft.com.au/projects/albatross/albatross/tug-tree.html

And the input tag name alias process is explained in point 3 of this
page:

  http://www.object-craft.com.au/projects/albatross/albatross/tag-input-alias.html

- Dave

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list