#!/usr/bin/python # # Copyright (c) 2007-2008 by Fred Morris Consult, Seattle WA USA # # Author: Fred Morris # Date: 25-Jan-2008 # # Licensed under the Perl Artistic License: "under the same terms # as Perl itself". """Concatenation Example Usage: Called as a CGI Script Demonstrates building a web page from three separate web pages by simple concatenation. The subtle deal with this one as opposed to the (true) nested example is that all of the handlers are inner handlers. The prologue and epilogue are build from the outer handler's template, the the outer handler's template is disabled. An additional subtlety of this one is that Prologue and Epilogue attempt to do things with the outer response body before the (asynchronous) gate. Therefore fetching the outer template asynchronously may (but is not guaranteed to) produce anomalous behavior. Parameters: None Version: 1.1 Modification History: FWM 20-Feb-2008 ...; Fetch inner templates asynchronously. """ import sys from snakestation.handlers import Nester, Inner class Inner1 (Inner): TEMPLATE_NAME = 'Inner' ASYNCHRONOUS_TEMPLATE = True class Inner2 (Inner): TEMPLATE_NAME = 'Inner2' ASYNCHRONOUS_TEMPLATE = True class Prologue (Nester): TEMPLATE_NAME = None def post_template(self): """Everything before inner is our part of the response. A NOTE ON STYLE: if this was done in before_prepare(), we could safely enable ASYNCHRONOUS_TEMPLATE in Outer. """ Nester.post_template(self) self.response.body_sent = True self.add_response(self.as_inner_response(self.response.body.split('%%inner%%')[0])) return class Epilogue (Nester): TEMPLATE_NAME = None def post_template(self): """Everything after inner2 is our part of the response. A NOTE ON STYLE: if this was done in before_prepare(), we could safely enable ASYNCHRONOUS_TEMPLATE in Outer. """ Nester.post_template(self) self.response.body_sent = True self.add_response(self.as_inner_response(self.response.body.split('%%inner2%%')[1])) return class Outer (Nester): """A toplevel handler containing nested handlers.""" TEMPLATE_NAME = 'Outer' # Uncommenting the following may produce an error because Prologue and # Epilogue access the body in post_template() which is before the gate. # ASYNCHRONOUS_TEMPLATE = True INNER_HANDLERS = (Prologue,Inner1,Inner2,Epilogue) def __init__(self,**args): """Send HTML""" Nester.__init__(self,**args) self.response.send_html() return def main(): handler = Outer() handler.template_map = { 'Outer' : 'http://flame.m3047.inwa.net/snakestation/outer.html', 'Inner' : 'http://flame.m3047.inwa.net/snakestation/inner.html', 'Inner2': 'http://flame.m3047.inwa.net/snakestation/inner-2.html', } handler.main() if __name__ == '__main__': main()