[albatross-users] Access to current/other page objects/modules

Gregory Bond gnb at itga.com.au
Fri Jan 24 11:04:16 EST 2003


Andrew,

Thanks for your response.  I was about to post a followup....

> Could the PageBase class copy the relevent data from the subclass into
> ctx.locals in the page_enter method? 

I thought of that, but you also have to add it the session so that page 
refreshes work right.   And it was still not clear how to access this 
information in a way that says "place an HTML nabvar here".

After a fair bit of experimenting I've come up with the following solution 
that seems to work OK:

- Do it with a new tag, as this will have access to the ctx for generating html
- The ctx object has a reference to the application object in ctx.app
- The ctx object also has the name of the current page in ctx.locals.__page__
- Alas, with the standard PageObjectMixin there is no way to get at the map of 
  names->page objects, because this is hidden by the funky handling of the 
  __page_objects name.  No other method of the mixin gives access to a 
  page object without undesriable side effects!
- This can be fixed with a patch to add a page_object() method the mixin, or 
  you can override the register_page() method in your app class and keep a 
  second parallel copy of the page_objects dict:

    def register_page(self, name, cls):
	self.page_objects[name] = cls
	SimpleSessionFileApp.register_page(self, name, cls)
	
    def page_object(self, name):
	return self.page_objects[name]

- The to_html() method of the navbar tag can recognise when the 
  descriptions have embedded albatross tags and treat them as a kind of mini 
  template.  This took the most hunting and fiddling and I'm not entirely 
  sure I've done it the best way....
- copy the ideas from the al-a tag to format the actual navbar links.

so my tag class looks like this:

class NavBar(EmptyTag):
    '''
    The main guts of the navbar, based on the @nav_to & @description
    page class members.
    
    Applications can subclass this and replace the to_html() and
    format_link() functions to change the look of the navbar.  They
    will need to (re-) register the tag in the application code to get
    the overriden version tho.    
    '''
    name = 'alx-navbar'
    
    def to_html(self, ctx):
	ctx.write_content('<!-- alx-navbar -->\n<hr>\n')	
	self.do_links(ctx)
	ctx.write_content('<!-- end alx-navbar -->\n')

    def do_links(self, ctx):
	self.format_link(ctx, 'restart', 'Start Again')
	if len(ctx.locals.back) > 0:
	    self.format_link(ctx, 'goback', 'Back')
	pclass = ctx.app.page_object(ctx.locals.__page__)
	for dest in pclass.nav_to:
	    dclass = ctx.app.page_object(dest)
	    desc = dclass.description
	    if desc.find("<al") >= 0:
		ctx.push_content_trap()
		Template(ctx, '<magic>', desc).to_html(ctx)
		desc = ctx.pop_content_trap()
	    self.format_link(ctx, dest, desc)

    def format_link(self, ctx, href, desc):
	ctx.write_content('<a href="%s?%s=1">%s</a>  \n' %
			  (ctx.current_url(), href, desc))





More information about the Albatross-users mailing list