Permalink
Browse files

updated with jinja2 templates

  • Loading branch information...
1 parent 98e7737 commit bd6aeacf90aac1f72034c723f6d04b27801b02a3 @ctb committed Feb 16, 2012
Showing with 76 additions and 14 deletions.
  1. +14 −14 meep_example_app.py
  2. +12 −0 templates/add_message.html
  3. +11 −0 templates/base.html
  4. +21 −0 templates/index.html
  5. +18 −0 templates/list_messages.html
View
@@ -3,6 +3,8 @@
import cgi
import meepcookie
+from jinja2 import Environment, FileSystemLoader
+
def initialize():
# create a default user
u = meeplib.User('test', 'foo')
@@ -12,6 +14,13 @@ def initialize():
# done.
+env = Environment(loader=FileSystemLoader('templates'))
+
+def render_page(filename, **variables):
+ template = env.get_template(filename)
+ x = template.render(**variables)
+ return str(x)
+
class MeepExampleApp(object):
"""
WSGI app object.
@@ -21,7 +30,7 @@ def index(self, environ, start_response):
username = 'test'
- return ["""you are logged in as user: %s.<p><a href='/m/add'>Add a message</a><p><a href='/login'>Log in</a><p><a href='/logout'>Log out</a><p><a href='/m/list'>Show messages</a>""" % (username,)]
+ return [ render_page('index.html', username=username) ]
def login(self, environ, start_response):
# hard code the username for now; this should come from Web input!
@@ -61,27 +70,17 @@ def logout(self, environ, start_response):
def list_messages(self, environ, start_response):
messages = meeplib.get_all_messages()
- s = []
- for m in messages:
- s.append('id: %d<p>' % (m.id,))
- s.append('title: %s<p>' % (m.title))
- s.append('message: %s<p>' % (m.post))
- s.append('author: %s<p>' % (m.author.username))
- s.append('<hr>')
-
- s.append("<a href='../../'>index</a>")
-
headers = [('Content-type', 'text/html')]
start_response("200 OK", headers)
- return ["".join(s)]
-
+ return [ render_page('list_messages.html', messages=messages) ]
+
def add_message(self, environ, start_response):
headers = [('Content-type', 'text/html')]
start_response("200 OK", headers)
- return """<form action='add_action' method='GET'>Title: <input type='text' name='title'><br>Message:<input type='text' name='message'><br><input type='submit'></form>"""
+ return [ render_page('add_message.html') ]
def add_message_action(self, environ, start_response):
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
@@ -121,6 +120,7 @@ def __call__(self, environ, start_response):
return fn(environ, start_response)
except:
tb = traceback.format_exc()
+ print tb
x = "<h1>Error!</h1><pre>%s</pre>" % (tb,)
status = '500 Internal Server Error'
View
@@ -0,0 +1,12 @@
+{% extends "base.html" %}
+
+{% block title %}meep: add message!{% endblock title %}
+
+{% block body %}
+
+<form action='add_action' method='GET'>
+Title: <input type='text' name='title'><br>
+Message:<input type='text' name='message'><br>
+<input type='submit'></form>
+
+{% endblock body %}
View
@@ -0,0 +1,11 @@
+<html>
+<head>
+{% block head %}
+<title>{% block title %}meeeeeeep!{% endblock title %}</title>
+{% endblock %}
+</head>
+<body>
+{% block body %}
+{% endblock %}
+</body>
+</html>
View
@@ -0,0 +1,21 @@
+{% extends "base.html" %}
+
+{% block body %}
+
+you are logged in as user: {{ username }}.<p>
+
+<a href='/m/add'>Add a message</a>
+
+<p>
+
+<a href='/login'>Log in</a>
+
+<p>
+
+<a href='/logout'>Log out</a>
+
+<p>
+
+<a href='/m/list'>Show messages</a>
+
+{% endblock body %}
@@ -0,0 +1,18 @@
+{% extends "base.html" %}
+
+{% block title %}meep: list messages!{% endblock title %}
+
+{% block body %}
+
+ {% for item in messages %}
+
+ id: {{ item.id }}<p>
+ title: {{ item.title }}<p>
+ message: {{ item.post }}<p>
+ author: {{ item.author.username }}<p>
+ <hr>
+ {% endfor %}
+
+<a href='../../'>index</a>
+
+{% endblock body %}

0 comments on commit bd6aeac

Please sign in to comment.