Spyce example

Bruce Webber <[email protected]>
Newsgroups gmane.comp.python.spyce.general
Message-ID <CF44087474CBAF4151E0B399@[192.168.1.100]>
I recently set up a small web site using Spyce, and thought I'd share some 
of the code which I believe shows the advantages of Spyce and the use of 
web standards.

This code is part of http://www.karda.org, which is for an open-source 
project that I have initiated. (The project is to create a flash card 
application to assist with memorization. There will be both a desktop 
version written in Python and a web version written in Spyce, sharing a 
common Python library.)

Typically a website has a navigation area (often on the left side or across 
the top). In my site this consists of a list of links that appear inside a 
"navigation box". I want the current page to appear not as a link but as 
bold text, to show that the visitor is already on the page. And I want this 
list of links to be maintained in one place. I don't want to add a page to 
the site and have to update every other page with the new link.

Using Spyce, I created a tag library which includes the tag writeNavHTML. 
Each page on the website begins with the line:

    [[.taglib from=kardawebtags.spy as=kwtag]]

and includes a line like:

    <kwtag:writeNavHTML currpage="Home" />

The attribute currpage is the name of current page (in this case, the Home 
page).

My tag library (kardawebtags.spy) provides the HTML code that is repeated 
on the web pages. It includes the line:

    [[import kardaweb ]]

and the following tag definition:

    [[.begin name=writeNavHTML singleton=True ]]
        [[.attr name=currpage ]]

        <div id="navbox">
            [[=kardaweb.writeLinkHTML(currpage) ]]
        </div>
    [[.end]]

kardaweb.py defines a tuple of navigation links and the writeLinkHTML() 
function:

    # navigation links
    tNavLinks = (('Home', 'index.spy'),
        ('Technology', 'tech.spy'),
        ('License', 'license.spy'),
        ('Contact', 'contact.spy'),
        ('Trac (Source Code Management)', 'http://trac.karda.org'))

    def writeLinkHTML(sCurrPage):
        sHTML = '<ul>\n'
        for tLink in tNavLinks:
            if tLink[0] == sCurrPage:
                sHTML += '<li class="current">' + tLink[0] + '</li>\n'
            else:
                sHTML += ('<li><a href="' + tLink[1] + '">' +
                    tLink[0] + '</a></li>\n')
        sHTML += '</ul>\n'
        return sHTML

The appearance of the navigation box and the links is controlled by the 
style sheet.

It's now easy to add a page to the site and update the tuple of navigation 
links, and all web pages are updated. This approach largely separates the 
content, style and logic.

--
Bruce Webber
[email protected]
http://brucewebber.us


-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.