Saturday, January 3, 2009

Google app engine templates and custom filters.

Last month I was messing with google app engine. That's right GAE (google app engine) and python. 

Still hardly can find any productivity gains from switching to python or as other will say "dynamic programming language" from java, but I still hope to see a "light at the end of the tunnel". With hope to experience some new, more dynamic and less restrictive thinking and software development, I'm still keep going by this new path.

Here is a new tiny challenge I got and how I'm going to deal with it. GAE uses template engine for view generation, what is greatly welcomed. Template engine and most of other code are borrowed from django, which is a high-level Python web framework. So this is a reason why I going to refer to django. Django Template engine has a nice feature which is called a template filter. Template filters are used to modify variables for display, this means that before variable value will be written to the output it can be formatted or modified in any way as it's needed.

For beginning I used this blog post as my starting point. It has a list of steps what is needed to get started. In order to get solution I like to dig a little dipper and understand what is going on. Here is a few questions what I wold like to know before I will do what I need.

# What are the differences between GAE and django template engine?
# How can I add my custom filters?
# What is a default set of filters in GAE?

Ok here are some my questions and answers to them.

What are the differences between GAE and django template engine?

First difference is a minor one. Location of Template class in django and GAE differs. Django uses django.template.Template class and GAE has it's own appengine.ext.template.Template class and it is just a simple wrapper class for the django Template class. This wrapper class handles all differences between django and app engine and makes programmers work easier.

How can I add my custom filters?

Google app engine and django uses different steps for custom filter registration. I had to create a new package, in my case I created a new package "common" and templatefilters.py file in it with content listed bellow. In case you want to look how can it be done in django here is a source of this file.

import urllib
from google.appengine.ext import webapp

def truncate(value,maxsize,stopper = '...'):
""" truncates a string to a given maximum
size and appends the stopper if needed """
stoplen = len(stopper)
if len(value) > maxsize and maxsize > stoplen:
return value[:(maxsize-stoplen)] + stopper
else:
return value[:maxsize]

def rb_quote_plus(value):
""" Replace special characters in string using
the %xx escape. Letters, digits, and the
characters '_.-' are never quoted. Also
replaces spaces by plus signs, as required
for quoting HTML form values. """

return urllib.quote_plus(value)

# get registry, we need it to register our filter later.
register = webapp.template.create_template_register()
register.filter(truncate)
register.filter(rb_quote_plus)


One last step is needed in your main.py file or any other fail with main method at the top level you have to add code listed bellow. This will instruct app engine to register new teplate filters from common.templatefilters.py file.

webapp.template.register_template_library('common.templatefilters')


Here are default list of filters in django template. In case if it's not enough and you need some more information what can be done with filters, information is provided in django documentation custom template tags and filters. This reference is for django version 1.0 and I guess something will not work because GAE uses 0.96 version as basis.

Conclusions
I was not able to find information in form I liked. This blog post is consequence of that. In general I liked how easy it's to extend and customize templates. I'm impressed how easy to get additional power in view layer without mixing business and UI logic in one place. Some features what I haven't tried yet, and I think they could not work in GAE, such as decorator @register.filter makes me excited. Once I found what and how filters can be added, I should say, this is an easiest way to add custom features to templates.

3 comments:

Anonymous said...

Great story you got here. It would be great to read a bit more about that topic. Thnx for posting that info.
Sexy Lady
Girls for companionship in London

blemish removal lotion said...

wow. lastly, I found one thing helpful for my paper to jot down about. that is attention-grabbing and helps me with more research within the future. Glad I discovered this blog.Thank you. And I do hope you will broaden a few of your ideas about this topic and I will positive come back and browse it. Thanks for the hassle and time.

Michael Dorf said...

Hi, thank you for your informative post. We've also written one recently on getting started with Google App Engine. Your readers may find it complementary to yours:

http://www.learncomputer.com/getting-started-with-google-app-engine/

Thanks again!