Need Python example of Template System

45 views
Skip to first unread message

Ayan

unread,
Oct 1, 2011, 1:42:29 AM10/1/11
to Google App Engine
Dear Friends,

Can you please provide me a very simple HELLO WORLD type python script
using Template System ?

Actually, whatever example i find in the internet or google forum, is
either too big and complex or not working at all.

Please help me out.

Ernesto Oltra

unread,
Oct 3, 2011, 11:11:16 AM10/3/11
to google-a...@googlegroups.com

Gillwill

unread,
Oct 4, 2011, 4:12:24 AM10/4/11
to Google App Engine
How about this:
(assumes you have the "helloworld.html" stored in the "templates"
subfolder of where your code is)


*****for main.py:
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
def get(self):
template_values = { 'textMessage': 'Hello World', }
path = os.path.join(os.path.dirname(__file__), 'templates/
helloworld.html')
self.response.out.write(template.render(path, template_values))

def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)

if __name__ == '__main__':
main()



****for helloworld.html:


<html>
<head>
<title>My Template</title>
</head>

<body>
<p> {{textMessage}} </p>
</body>
</html>

Gillwill

unread,
Oct 4, 2011, 4:23:07 AM10/4/11
to Google App Engine
Oops!

Of course the code under "def get(self):" should be indented
accordingly.




= = = = = = = = = =

How about this (assumes you have the "helloworld.html" stored in the
"templates" subfolder of where your code is)

for main.py:

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
def get(self):
template_values = { 'textMessage': 'Hello World', }
path = os.path.join(os.path.dirname(__file__), 'templates/
helloworld.html')
self.response.out.write(template.render(path, template_values))

def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)

if __name__ == '__main__':
main()



Murph

unread,
Oct 4, 2011, 4:15:41 PM10/4/11
to google-a...@googlegroups.com
You should probably use some other template system (e.g. django.template), as webapp.template is going to be deprecated soon.

JH

unread,
Oct 4, 2011, 4:47:55 PM10/4/11
to Google App Engine
consider using jinja2
http://jinja.pocoo.org/docs/
Reply all
Reply to author
Forward
0 new messages