Django Template Language in JavaScript

Written by on February 7th, 2008 in Ajax News.

I am learning at lot at the Dojo Developers Day one. Whenever I met Dojo folks I get the impression that there are 55 gems in the library that I have no idea about!

One of them is Neil Roberts implementation of the Django template language in JavaScript.

His work lives in dojox.dtl and you can check out a simple demo that renders, and rerenders a page with new content as you add it:

HTML:

<html>
<head>
        <title>Demo using dojox.dtl._Templated</title>
  <script type=”text/javascript” src=”../../../dojo/dojo.js”
                djConfig=”isDebug: true, parseOnLoad: true”></script>
  <script type=”text/javascript” src=”../../../dijit/dijit.js”></script>
  <script type=”text/javascript”>
        dojo.require("dojox.dtl._Templated");

        dojo.declare("Fruit", [dijit._Widget, dojox.dtl._Templated], {
                oldRepl: "some value for testing the old style template substitution",
                _dijitTemplateCompat: true,
                items: ["apple", "banana", "orange"],
                keyUp: function(e){
                        if (e.keyCode == dojo.keys.ENTER) {
                                var i = dojo.indexOf(this.items, e.target.value);
                                if (i != -1){
                                        this.items.splice(i, 1);
                                } else {
                                        this.items.push(e.target.value);
                                }
                                e.target.value = "";
                                this.render();
                                dojo.query("input", this.domNode).forEach("item.focus();");
                        }
                },
                templateString: ‘<div><input dojoAttachEvent=”onkeyup: keyUp”/><ul>{% for item in items %}<li>{{ item }} ${oldRepl}</li>{% endfor %}</ul></div>’
        });

  dojo.require("dojo.parser");
  </script>

        <body>
                <div dojoType=”Fruit”></div>.
        </body>

</head>
</html>
 

You can also see another interesting example which is a blog view. Take a view source on that puppy.

Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/231164624/django-template-language-in-javascript

Comments are closed.



Site Navigation