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

Dave Cole djc at object-craft.com.au
Fri Jan 24 15:41:03 EST 2003


> 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

If we ever get around to updating our documentation we will have a new
release...  In the current code we have made the execution context
available to the template expressions by doing this:

class NamespaceMixin:
    def eval_expr(self, expr):
        self.locals.__ctx__ = self
        try:
            return eval(expr, self.__globals, self.locals.__dict__)
        finally:
            del self.locals.__ctx__

> - 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!

Maybe we should add your extra method to PageObjectMixin.

> 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))

The only thing you might want to do is turn dclass.description into a
method which returns either a string or a template.  This will allow
you to cache templates in the dclass if you deploy on mod_python.

	for dest in pclass.nav_to:
	    dclass = ctx.app.page_object(dest)
	    desc = dclass.description()
	    if isinstance(desc, Template):
		ctx.push_content_trap()
		desc.to_html(ctx)
		desc = ctx.pop_content_trap()
	    self.format_link(ctx, dest, desc)

- Dave

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list