Adding a page list

Most wikis have a feature that lets you view an index of the pages. To add one, we’ll start with a new template, pagelist.html. We’ll copy page.html so that we don’t have to write the boilerplate.

cd wiki20/templates
cp page.html pagelist.html

After editing, our pagelist.html looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">

  <xi:include href="master.html" />

<head>
  <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
  <title>Page Listing - The TurboGears 2 Wiki</title>
</head>

<body>

<div class="main_content">
<h1>All Pages</h1>
<ul>
    <li py:for="pagename in pages">
        <a href="${tg.url('/' + pagename)}"
            py:content="pagename">Page Name Here.</a>
    </li>
</ul>
Return to the <a href="/">FrontPage</a>.

</div>

</body>
</html>

19-22 This section represents the Genshi code of interest. You can guess that the py:for is a python for loop, modified to fit into Genshi’s XML. It iterates through each of the pages (which we’ll send in via the controller, using a modification you’ll see next). For each one, Page Name Here is replaced by pagename, as is the URL.

We must also modify controller/root.py to implement pagelist and to create and pass pages to our template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Main Controller"""
from wiki20.lib.base import BaseController
from tg import expose, flash, require, url, request, redirect
from pylons.i18n import ugettext as _, lazy_ugettext as l_
from tg import redirect, validate
from wiki20.model import DBSession, metadata
from wiki20.controllers.error import ErrorController
from wiki20.model.page import Page
import re
from docutils.core import publish_parts
from sqlalchemy.exceptions import InvalidRequestError

wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")

class RootController(BaseController):
    error = ErrorController()

    @expose('wiki20.templates.page')
    def default(self, pagename="FrontPage"):
        try:
            page = DBSession.query(Page).filter_by(pagename=pagename).one()
        except InvalidRequestError:
            raise redirect("notfound", pagename = pagename)
        content = publish_parts(page.data, writer_name="html")["html_body"]
        root = url('/')
        content = wikiwords.sub(r'<a href="%s\\1">\\1</a>' % root, content)
        return dict(content=content, wikipage=page)

    @expose(template="wiki20.templates.edit")
    def edit(self, pagename):
        page = DBSession.query(Page).filter_by(pagename=pagename).one()
        return dict(wikipage=page)

    @expose('wiki20.templates.about')
    def about(self):
        return dict(page='about')
        
    @expose("wiki20.templates.edit")
    def notfound(self, pagename):
        page = Page(pagename=pagename, data="")
        DBSession.add(page)
        return dict(wikipage=page)
        
    @expose("wiki20.templates.pagelist")
    def pagelist(self):
        pages = [page.pagename for page in DBSession.query(Page).order_by(pagelist)]
        return dict(pages=pages)

45-47 We select all of the Page objects from the database, and order them by pagename.

We can also modify page.html so that the link to the page list is available on every page (line 28):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:py="http://genshi.edgewall.org/"
    xmlns:xi="http://www.w3.org/2001/XInclude">


    <xi:include href="header.html" />
    <xi:include href="footer.html" />
    <xi:include href="master.html" />

    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/>
        <title>${wikipage.pagename} - The TurboGears 2 Wiki</title>
    </head>

    <body>

        <div class="main_content">
            <div style="float:right; width: 10em;"> Viewing
                <span py:replace="wikipage.pagename">Page Name Goes Here</span>
                <br/>
                You can return to the <a href="/">FrontPage</a>.
            </div>
            <div py:replace="XML(content)">Formatted content goes here.</div>
            <a href="/edit/${wikipage.pagename}">Edit this page</a>
            <a href="/pagelist">View the page list</a>
        </div>

    </body>
</html>

You can see your pagelist by clicking the link on a page or by going directly to http://localhost:8080/pagelist.

Previous topic

Hey, where’s the page?

Next topic

Using your models outside of TurboGears

This Page