[cse491] templates

C. Titus Brown ctb at msu.edu
Sun Nov 22 17:42:11 PST 2009


On Sun, Nov 22, 2009 at 03:34:12PM -0500, Ryan Kelly wrote:
> 	I'd like to get a little more detail on part 3 of the homework... Are you asking us to integrate the message board into the jinja2 templating we did last homework? Or is this completely different? I'm having a hard time thinking about how I could use this app along with the fileserver app to serve the images and css of the template. I could be way off base though so any elaboration would be helpful. Thanks!

Hi Ryan,

there are a couple of ways to do it...

first, rather than returning a 404/not found when there's no match to
the requested url in the function dictionary, ask FileServer to handle
the request.  (A few more details about composing WSGI applications follow.)
This will then let your meep application reference files in your 'html'
subdirectory directly, as long as they don't conflict with a URL that
meep handles explicitly.

second, call retrieve a template file & call jinja2.render explicitly in
each meep app function, e.g. have 'index' get template 'meep_index.html'
(which inherits from base.html) and render it.

To verify that the first part is working, just try requesting a file that's in
your 'html' directory.  If you get those file contents back, then it's working.

The second part should give you a beaoooootiful Web page.

--

I discussed this in class but I think everyone was puzzled about what I
was talking about -- WSGI app composition.

Suppose you have two apps, one called 'hello_app' and one called 'form_app'.
hello_app returns 'hello, <url>' for all URLs.  form_app returns the contents
of 'form.html' when '/form.html' is requested, and runs the function
'process_form' when '/process_form' is requested.  form_app returns '404'
for all other URLs.

You could build a *composite* WSGI app, once created by composing those two
apps, by doing something like this:

def composite_app(environ, start_response):
  url = environ['PATH_INFO']
  if url.startswith('/form.html') or url.startswith('/process_form'):
     return form_app(environ, start_response)
  else:
     return hello_app(environ, start_response)

This new app delegates calls to /form.html and /process_form to form_app,
and otherwise calls hello_app to process them.  I'm not sure how to describe
it -- the code is pretty self-explanatory, isn't it?

You can do exactly this in your meep_app: have meep functions handle all
of the message board stuff, and pass anything that it doesn't explicitly
know how to handle on to FileServer.

cheers,
--titus
-- 
C. Titus Brown, ctb at msu.edu



More information about the cse491-fall-2009 mailing list