[albatross-users] Two questions (client-side production of static pages)

Dave Cole djc at object-craft.com.au
Fri Jun 28 10:45:05 EST 2002


>>>>> "Andy" == Andy Gimblett <gimbo at ftech.net> writes:

Andy> Hi there, First, let me say I've been using Albatross for a
Andy> little while now to power my weblog (http://gimbo.org.uk/) and I
Andy> really like it: it certainly seems to achieve the stated aim of
Andy> staying "out of the way" and letting me get on with what I want
Andy> to do in a reasonably obvious manner.

It is nice to see someone using the template processing in a
standalone manner.  We wanted to make the application functionality
optional.

Andy> I'm (currently) only using Albatross on the client-side.
Andy> Specifically, I'm using its templating capabilities to build
Andy> static pages which then get uploaded to my ISP's web server.
Andy> There are a couple of things I've had problems with.  I've fixed
Andy> one (sort of), but the other leaves me stumped.

Andy> The first question/point: I want to be able to produce output to
Andy> a file, not just stdout (don't want to use redirection as using
Andy> stdout for other purposes).  Now, I've achieved this by patching
Andy> ExecuteMixin and SimpleContext in the following "quick and
Andy> dirty" manner:

Andy> class ExecuteMixin:
Andy>     '''Manages a template execution context
Andy>     '''
Andy>     def __init__(self, filehandle=sys.stdout):
Andy>         self.__macro_stack = []
Andy>         self.__trap_stack = []
Andy>         self.reset_content()
Andy>         self.outputfilehandle = filehandle
Andy> 
Andy>     def send_content(self, data):
Andy>         self.outputfilehandle.write(data)
Andy>         self.outputfilehandle.flush()
Andy> 
Andy> class SimpleContext(NamespaceMixin, ExecuteMixin, ResourceMixin,
Andy>                     TemplateLoaderMixin, StubRecorderMixin,
Andy>                     StubSessionMixin):
Andy>     def __init__(self, template_path, outputfile=sys.stdout):
Andy>         import tags
Andy>         NamespaceMixin.__init__(self)
Andy>         ExecuteMixin.__init__(self, outputfile)
Andy>         ResourceMixin.__init__(self)
Andy>         TemplateLoaderMixin.__init__(self, template_path)
Andy>         apply(self.register_tagclasses, tags.tags)
Andy>         self.set_globals(_caller_globals(2))

Andy> This works for me, but I was wondering:

Andy>   - Can anyone think of a better way to achieve this?

Have you tried this:

ctx.push_content_trap()
# run template(s)
html = ctx.pop_content_trap()

If you refer to the request processing flow in:

    http://www.object-craft.com.au/projects/albatross/albatross/fig-presimpexec.html

you will note that the execution of templates and tags generates a
stream of content fragments which accumulate in the context.  Inside
the execution context there is a simple list of strings.  When the
context is flushed a simple string.join() is performed on that list.

Calling push_content_trap() saves the current list of fragments on a
stack and then clears the list.  When you call pop_content_trap() the
contents of the list and joined and returned and the previous fragment
list is restored from the stack.  This is used internally for
arguments to macro expansion and sometimes for options to the select
tag.

Andy>   - Is this (or a better solution) something that could be
Andy> folded into the offical distribution, so I needn't maintain my
Andy> own fork?  :-) It seems a useful capability to me.

Hopefully the content trap is what you need.

    http://www.object-craft.com.au/projects/albatross/albatross/mixin-execute.html

Andy> My second question, which I haven't been able to solve, regards
Andy> macros.  I have pages which are based on a master template,
Andy> along these lines:

Andy> master.html:
Andy> 
Andy>     <al-macro name="master">
Andy>       Stuff stuff stuff
Andy>       <al-usearg name="body">
Andy>       Blah blah blah
Andy>     </al-macro>
Andy> 
Andy> somepage.html:
Andy> 
Andy>     <al-expand name="master">
Andy>     <al-setarg name="body">
Andy>         Foo bar, foo bar, foo bar...
Andy>     </al-setarg>
Andy>     </al-expand>

Andy> Make sense?

Andy> Now, what I'd _really_ like is to have a number of templates
Andy> which are used to build each page.  In particular, a large chunk
Andy> of master.html is a sidebar table which is rather large: I'd
Andy> love to be able to break that out into a seperate template then
Andy> bring it in in either master.html or better yet, somepage.html
Andy> (then I could have a choice of sidebars for different pages, all
Andy> of which use the same master).

Andy> My instinct tells me there should be a way to do this but I
Andy> haven't been able to find it.  Can anyone clue me in?

Andrew has been working on something similar here.  Currently the
naming of macros is static and it looks like you need some kind of
dynamic macro capability.

The other way to do it might be to specify the sidebar when you expand
outermost macro.

master.html:

    <al-macro name="master">
      <al-usearg name="sidebar">
      Stuff stuff stuff
      <al-usearg name="body">
      Blah blah blah
    </al-macro>

somepage.html:

    <al-expand name="master">
    <al-setarg name="sidebar">
     <al-expand name="sidebar-with-bells-on"/>
    </al-setarg>
    <al-setarg name="body">
        Foo bar, foo bar, foo bar...
    </al-setarg>
    </al-expand>

Or even somepage.html:

    <al-expand name="master">
    <al-setarg name="sidebar">
     <al-include name="sidebar-with-bells-on.html">
    </al-setarg>
    <al-setarg name="body">
        Foo bar, foo bar, foo bar...
    </al-setarg>
    </al-expand>

Andy> That's it - thanks again for a cool product.

Lets hope we can make it even more cool.

- Dave

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




More information about the Albatross-users mailing list