9: Template Dictionaries

< Previous Step Next Step >


There is a dictionary (a Python data structure) passed as a parameter to every render function. This is added to the dictionary within the render function itself. This is then passed to the Django template renderer.

Dictionary values that apply to all calls are kept within the render function. Those that change are in the calls to the render function.

Both the Common Templates and the Difference Templates have Dictionaries, loaded with appropriate values before they are called. The Common Renderers for the Common Templates are called with the Common Dictionaries. The results are passed to the Difference Dictionaries for use by the Difference Renderer of the Difference Template.

There are two Common Dictionaries per page call: one that is passed to the renderer, and one within the renderer. Otherwise, there would be no point in separating the renderer.

The same is true of the Difference Dictionaries.


So, for example, the handler class SplashPage now acts as a conduit between the common template function and the difference function for the splash page:

class SplashPage(webapp.RequestHandler):
def get(self):

# dictionary for common templates
template_values = {
'title': 'splash',
'url': '/'
}

# render common templates
header = header_render(template_values)
footer = footer_render(template_values)

# add results to dictionary, for difference template
template_values['header'] = header
template_values['footer'] = footer

# render difference template
self.response.out.write (splash_page_render(template_values))

The user page is a little different: there are many users, differentiated by a key passed to the handler:

class UserPage(webapp.RequestHandler):
def get(self, key):

# get current user text
user = users.get_current_user()

# dictionary for common templates
template_values = {
'title': user,
'url': 'user/' + key
}

# render common templates
header = header_render(template_values)
footer = footer_render(template_values)

# add results to dictionary, for difference template
template_values['header'] = header
template_values['footer'] = footer

# render difference template
self.response.out.write (user_page_render(template_values))


The admin page is a single, lioke the splash page:

class AdminPage(webapp.RequestHandler):
  def get(self):

    # dictionary for common templates
    template_values = {
      'title': 'admin',
      'url': 'admin'
    }

    # render common templates
    header = header_render(template_values)
    footer = footer_render(template_values)

    # add results to dictionary, for difference template
    template_values['header'] = header
    template_values['footer'] = footer

    # render difference template
    self.response.out.write (admin_page_render(template_values))

... and then the group page is much like the user page, at this point.

class GroupPage(webapp.RequestHandler):
  def get(self, key):

    # state switch: user's membership in group

    # dictionary for common templates
    template_values = {
      'title': key,
      'url': 'user/' + key
    }

    # render common templates
    header = header_render(template_values)
    footer = footer_render(template_values)

    # add results to dictionary, for difference template
    template_values['header'] = header
    template_values['footer'] = footer

    # render difference template
    self.response.out.write (group_page_render(template_values))

Comments