Showing posts with label Django Templates. Show all posts
Showing posts with label Django Templates. Show all posts

Sunday, August 23, 2009

Accessing Django Template filters in Google App Engine

This is a quick one. Django templates (including the version 0.96 supported for the Google App Engine Webapp framework) have this notion of output filters. Within any Django template you can output one of the variables passed to the rendering command using a simple markup within your HTML. This example:

<p>{{myvar}}</p>

outputs the variable myvar. One of the great inclusions in the templating language is filters. Filters are a range of formatting and transformation routines built into the templating language. This means you can format dates, truncate data, strip or escape HTML tags, replace linefeeds with paragraph tags and more within your template, not your calling code, which keeps your controller and model focused on their role and not overly processing data for the view. This makes your code cleaner. Filters can also be chained together to combine effects. For example:

<p>{{myvar|striptags|truncatewords:"10"}}</p>


This code removes html tags for the myvar variable and then truncates the result to 10 complete words. All of this goodness is available within the Webapp framework provided with Google App Engine. Read the documentation of the 0.96 version of Django Templates (used in App Engine).

Segregating the data transformation for view logic into the view is mostly very useful and powerful, but sometimes you need the functionality in filters outside of a template. For example the slugify filter creates a URL-safe string. Using slugify within the template is easy, but what happens when you need to redirect to the slugified URL? You could replicate the functionality outside of the template, but as well as duplicating code you are introducing the possibility that your algorithm will not match the one used by the built-in filter.

It turns out that accessing the filters from Python is very easy. The filters exist as simple functions that with the correct import are exceedingly easy to use. Conveniently, there is no change in the import path for Google App Engine as opposed to Django. Import the django.templates object defaultfilters and you are ready to go:

def slugify(string):
'''returns a URL-safe slug based on the provided string
Reuses the django template filter
'''
from django.template import defaultfilters
return defaultfilters.slugify(string)


Pretty simple, right?

One thing I will not talk about here (mostly since I haven't done it yet) is the process for supplementing the built in filters with your own custom filters. For those of you who are curious, read this informative blog entry.

If you have any feedback on this technique, let me know.

Saturday, July 18, 2009

Iterating over arrays in Django Templates for Google App Engine

This is a quick post to help anyone else who might be confused about how to iterate over arrays within Django Templates for Google App Engine.

Django templates are great. I am still exploring a lot of the flexibility they allow. One of the mundane features of the templating engine is the ability to iterate over data structures such as lists and arrays. This is fundamental to any web page showing a data table, list, data driven navigation or any similiar display. The great thing about Django templates is that this has a natural and familiar syntax. For a list, you write something along the lines of:


{% for item in list %}
Displaying or doing something with an item: {{ item }}
{% endfor %}


This iterates across all of the members in the list sequence, repeating the static text and item value for each item's value. Simple.

What about a dictionary? A dictionary is different from a list in many ways, but from the point of view of presenting data from a dictionary one of the key differences is that as you iterate you frequently want both the key and the value.

I faced this recently when beginning coding on my Google App Engine application and found the Django 0.96 template documentation Google Code refers to lacking an example of how iterating across a dictionary within the template is performed.

I found this post which succinctly demonstrated the modern Django Template syntax:


{% for key,value in dictionary.items %}
{{key}} is the key for the value {{ value }}
{% endfor %}



This was pretty intuitive and made sense. Unfortunately it does not work in Google App Engine. Why? Google App Engine still uses Django templates from Django 0.96, and this is one of the features added in the 1.0 release.

For those of us wanting to iterate over dictionaries in Google App Engine we need to use this somewhat less pretty but still functional format available in 0.96:


{% for item in dictionary.items %}
{{ item.0 }} is the key for value {{ item.1 }}
{% endfor %}


I can see why the Django folks improved the syntax.

Anyway, thanks go to Dan Ellis on this thread of the Django Google Group for pointing this out.