Login Register

Writing a Widget

We'll bypass how to use the raw HtmlTemplate object (we'll get into that later) and explain how to write a widget using Dojo's implementation of the Django Template Language.

Both of the solutions covered here work almost exactly like dijit._Templated, which is covered elsewhere in the book. To use the text version, mix in dojox.dtl._Templated and to use the HTML version, mix in dojox.dtl._HtmlTemplated.

These objects will use templatePath, templateString, and use the dojoAttachPoint and dojoAttachEvent node attributes.

They add a single function: render. This function is to be used in the event of re-rendering. One of the main reasons for re-render would be if not all data was available during instantiation. The template will be rendered during creation even if you don't call the render function.

The template will be rendered using the widget object as its context. If you don't want this behavior, you can pass your own Context object to the render function.

dojo.require("dojox.dtl._Templated");

dojo.declare("demo", [dojox.dtl._Widget, dojox.dtl._Templated] {
  templateString: "<div>I like eating {{ fruit }}</div>",
  postCreate: function(){
    this.fruit = "apple";
    this.render();
  }
});

Good tutorial

I've put a good tutorial up at http://shaneosullivan.wordpress.com/2008/03/03/writing-a-django-template...

It goes through writing a template that works with a dojo data store, and writing the widget to render it.