Django Template Language

This is one of the coolest features in Dojo. I like it because, just like other template patterns, you can accomplish a lot with data driven programming. Using DTL makes processing data easy and you can replace the template conditionally in code. Taking a snippet from the DojoCampus, you can see the power of using templates by reading the code – see below.


  //Load the NodeList extension for DTL
  dojo.require("dojox.dtl.ext-dojo.NodeList");

  dojo.addOnLoad(function(){
    // Retrieve the nodes (there's only one) using dojo.query
    // Apply the template, which is loaded from a remote file
    dojo.query("#dtlNode").dtl("featureexplorer/Dojox/DTL/nodelist.txt",
      { items: ["apple", "banana", "pear"] });
  });

An unordered list is created here from a JavaScript array

The URL (featureexplorer/Dojox/DTL/nodelist.txt) has the following content:

    {% for item in items %}
  • {{ item }}
  • {% endfor %}

You can see the code uses the text snippet to show the HTML in a “template” manner. This means we could have used a condition to show the data in a different way by pointing to another template. In this case it creates a DIV wrapper and then a list, where each entry in the array ends up being a single entry in the list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s