Permalink
Browse files

fixed up Mako BeforeRender example

  • Loading branch information...
1 parent eb393b4 commit bad1d5e3f74ef643866214227792ae4195b7df48 @mmerickel mmerickel committed Mar 25, 2013
Showing with 20 additions and 18 deletions.
  1. +20 −18 templates/templates.rst
View
38 templates/templates.rst
@@ -58,31 +58,33 @@ The value inserted into the template as the result of this statement will be
You can add more imports and functions to ``helpers.py`` as necessary to make
features available in your templates.
-Usage in Mako Templating:
-If you wanted to change templates using %inherit based on if a user was logged
-in you could do the following:
+Using a BeforeRender Event to Expose a Mako ``base`` Template
+-------------------------------------------------------------
-.. code-block:: python
+If you wanted to change templates using ``%inherit`` based on if a user was
+logged in you could do the following:
- @subscriber(BeforeRender)
- def add_base_template(event):
- request = event.get('request')
- if request.user:
- base = 'blaster:templates/logged_in_layout.mako'
- event.update({'base': base})
- else:
- base = 'blaster:templates/layout.mako'
- event.update({'base': base})
+.. code-block:: python
-and then in your mako file you can call %inherit like so
+ @subscriber(BeforeRender)
+ def add_base_template(event):
+ request = event.get('request')
+ if request.user:
+ base = 'myapp:templates/logged_in_layout.mako'
+ event.update({'base': base})
+ else:
+ base = 'myapp:templates/layout.mako'
+ event.update({'base': base})
-.. code-block:: python
+And then in your mako file you can call %inherit like so::
<%inherit file="${context['base']}" />
-.. note::
-You must call the variable this way because of the way Mako works.
-It will not know about any other variable until after %inherit is called.
+You **must** call the variable this way because of the way Mako works.
+It will not know about any other variable other than ``context`` until after
+``%inherit`` is called. Be aware that ``context`` here is not the Pyramid
+context in the traversal sense (which is stored in ``request.context``) but
+rather the Mako rendering context.
Using a BeforeRender Event to Expose Chameleon ``base`` Template

0 comments on commit bad1d5e

Please sign in to comment.