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 is the module containing the application class. It also deploys the application as a CGI.

from albatross import RandomModularApp
from albatross import cgiapp
class App(RandomModularApp):
    def __init__(self):
            self,             base_url = '',
            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()

Points of interest:

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'.

   1 def page_display(ctx):
   2     '''
   3     Decides what to display based on ctx.locals.content_path and renders
   4     the content using the content.html template.
   5     '''
   7     # Break the path into parts
   8     parts = ctx.locals.content_path.split('/')
  10     # Build a breadcrumb trail, you would probably turn the path's parts
  11     # into something a bit more meaningful.
  12     breadcrumbs = []     
  13     for i in range(len(parts)):
  14         breadcrumbs.append((parts[i].capitalize(), '/'.join(parts[:i+1])))
  15     ctx.locals.breadcrumbs = breadcrumbs
  17     # Load the content
  18     ctx.locals.content = 'Content for %s' % (parts[-1] or 'root')
  20     ctx.run_template('content.html')


      Albatross Demo: RandomPageModuleMixin.get_page_from_uri()
      You are here:
      <al-a href="">Home</al-a>
      <al-for iter="crumb" expr="breadcrumbs">
        &nbsp;::         <al-a expr="crumb.value()[1]">
          <al-value expr="crumb.value()[0]" />
      <strong>requested content path:</strong>
      <al-value expr="content_path" />
      <al-value expr="content" />

None: get_page_from_uri()_example (last edited 2011-02-15 06:05:17 by localhost)