python functionality...
import templates as _templates
class _Page (object) :
"""
A page is kind of like a controller, I guess
"""
# the page title, used in the HTML <title>
title = None
# the page name, used in the menu
name = None
# the page path, used in the URL
path = None
def __init__ (self, req, path_suffix) :
self.req = req
self.path_suffix = path_suffix
def _build_template (self, template_class) :
tpl = template_class(searchList=[self.req])
tpl.page_title = self.title
tpl.page_name = self.name
tpl.page_path = self.path
tpl.page_menu = root_menu
tpl.page = self
tpl.page_children = self.get_children()
return tpl
def get_children (self) :
"""
Returns a list of page objects that are children of this one. May return None if there are none
"""
return None
def get_response_code (self) :
"""
Returns the HTTP response code to be used
"""
return 200
def render_template (self) :
"""
Returns an instance of Cheetah.Template, prepopulated with whatever variables it needs, ready to be rendered
"""
abstract
class StaticHTML (_Page) :
# the path to the .html file
file_path = None
def __init__ (self, *args) :
super(StaticHTML, self).__init__(*args)
# open the .html and read in the contents
fh = open(self.file_path, "r")
self.file_data = fh.read()
fh.close()
def render_template (self) :
tpl = self._build_template(_templates.layout)
tpl.page_content = self.file_data
return tpl
def html_page (_file_path, _title, _name, _path) :
class _anon_html_page (StaticHTML) :
file_path = _file_path
title = _title
name = _name
path = _path
return _anon_html_page
class Main (_Page) :
"""
Main page with simple stuff
"""
title = "Main Page"
name = "Main"
path = ""
def render_template (self) :
return self._build_template(_templates.main)
class Error404 (_Page) :
title = "Error 404 - Not Found"
def get_response_code (self) :
return 404
def render_template (self) :
return self._build_template(_templates.error_404)
# load HTML pages
About = html_page("pages/about.html", "About", "About", "about")
pages = {
'main': Main,
'about': About,
}
root_menu = [
Main,
About
]
def find (req) :
"""
This finds the page to use for the given req and reuturns an instance of it
"""
for prefix, suffix in req.page_name_prefixes() :
if prefix in pages :
return pages[prefix](req, suffix)
return Error404(req, req.page_path)