= Introduction = A small refactoring for Albatross 1.10 allows an Albatross application to provide its own scheme for mapping a URL to a page module. By overriding the get_page_from_uri() method (pulled in from the RandomPageModuleMixin) in your application class you can decode the URL in any way you choose. This mechanism can potentially be used for all sorts of magical mapping schemes but the most obvious use is to process everything below a certain URL path with the same page module. The following example is a brief demonstration of how you might write an Albatross application whose only purpose is to publish content based on the requested URL. Note that the application is not restricted to sending HTML to the browser, the requested URL could just as easily be for an image, CSS stylesheet, etc. See [[Sending_non-HTML_content]] for an example. = Application Code = == app.py == app.py is the module containing the application class. It also deploys the application as a CGI. {{{ #!/usr/bin/python from albatross import RandomModularApp from albatross import cgiapp class App(RandomModularApp): def __init__(self): RandomModularApp.__init__( self, base_url = 'app.py', page_path = '.', start_page = None, secret = '-=secret=-') def get_page_from_uri(self, ctx, uri): ''' Clean up the requested URI a little and then use the 'content' page module to process all requests. ''' # Let the mixin do all the hard work content_path = RandomModularApp.get_page_from_uri(self, ctx, uri) if content_path is None: content_path = '' # Removing slashes from start and end content_path = content_path.strip('/') # Record in the locals ctx.locals.content_path = content_path # Always process with the content 'page' return 'content' app = App() app.run(cgiapp.Request()) }}} Points of interest: * start_page = None. For this application the start_page is completely irrelevant as all URLs are mapped to the same page module. * get_page_from_uri(). I think the comments explain it all but all the method is doing is cleaning up the URI path to make sure it looks nice and passing it through to content.py in ctx.locals.content_path. == content.py == This is the where any database or file system access would be performed to turn content_path into real data for display. Purely so the function does ''something'' interesting, it builds a breadcrumb trail and uses the last part of the path as the content or 'root'. {{{ #!python def page_display(ctx): ''' Decides what to display based on ctx.locals.content_path and renders the content using the content.html template. ''' # Break the path into parts parts = ctx.locals.content_path.split('/') # Build a breadcrumb trail, you would probably turn the path's parts # into something a bit more meaningful. breadcrumbs = [] for i in range(len(parts)): breadcrumbs.append((parts[i].capitalize(), '/'.join(parts[:i+1]))) ctx.locals.breadcrumbs = breadcrumbs # Load the content ctx.locals.content = 'Content for %s' % (parts[-1] or 'root') ctx.run_template('content.html') }}} == content.html == {{{ Albatross Demo: RandomPageModuleMixin.get_page_from_uri()

You are here: Home  ::

requested content path:

content:

}}}