In my perfect world, all data I get from a server will be JSON, and I
can format it how I please.  Doing that with jQuery is somewhat verbose
at the moment, leaving me with awesome code like this:

        var headlines = $(Journals.HeadlinesContainer);
        for(i in json){
            var li = $.LI({ Class: json[i].ContainerFileName });
            var a = $.A().href(json[i].Url);
 
headlines.append(li.append(a.append($.TEXT(json[i].Title))));
        }

The Yahoo UI extensions (Jack Slocum rocks) have a neat Template class
that makes this a little easier, so I went ahead and jQueryfied it. 

My function now looks like:

$(Journals.HeadlinesContainer).appendTemplated(
        '<li class="#{ContainerFileName}"><a
href="#{Url}">#{Title}</a></li>',
        json
        );

Which is way nicer, IMO. :)  

Have a look at the demo here:
http://mubble.net/jquery/jquery.template.html

The template plugin itself is here:
http://mubble.net/jquery/jquery.template.js

For large results sets, you can pass a { 'compiled' : true } to the
appendTemplated function, which turns the template into a javascript
function rather than running a regex on every iteration.

-Kurt

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to