Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make sessions global in templates

  • Loading branch information...
commit 9f9048a9905e495340fcc9fb7dd4d9dae5f764a4 1 parent 3229215
@qbuben qbuben authored anandology committed
Showing with 15 additions and 2 deletions.
  1. +15 −2 cookbook/sessions_with_subapp.md
View
17 cookbook/sessions_with_subapp.md
@@ -20,7 +20,7 @@ In your app.py (or main.py) initialize your session like this:
session = web.session.Session(app, web.session.DiskStore('sessions'),
initializer = {'test': 'woot', 'foo':''})
-.. and create a processor via web.loadhook
+.. and create a processor via web.loadhook:
def session_hook():
web.ctx.session = session
@@ -30,4 +30,17 @@ In your app.py (or main.py) initialize your session like this:
.. and now in your 'sub-apps' (like sub-app.py) you can access the the session information like this:
print web.ctx.session.test
- web.ctx.session.foo = 'bar'
+ web.ctx.session.foo = 'bar'
+
+.. if you want to make sessions avaible in templates you should change session.hook function like this:
+
+ def session_hook():
+ web.ctx.session = session
+ web.template.Template.globals['session'] = session
+
+.. and now you can write in template something like this:
+
+ $session.get('logged_in', ''):
+ <p>You are $session.username</p>
+ $else:
+ <p>You are not logged in</p>
Please sign in to comment.
Something went wrong with that request. Please try again.