Helma Logo
main list history
previous version  overview  next version

Version 24 by zumbrunn on 19. December 2005, 10:48


    // function at Site.prototype.views.list.control(that)
    if (this.topics.count()) {
        that.topics = {};
        for (var topic in this.topics) {
            that.topics.name = this.topics[topic].name;
            that.topics.count = this.topics[topic].comments.count();


    <table border="1">
        <tr ifcheck="topics"
            <th lookup="true">Topic Name</th>
            <th lookup="true">Comment Count</th>
        <tr forloop="topics" class="rowcolor{ $topics %% 'rowcolor'+ ($topics % 2 }2) %%"
            <td>{ <td>%% topics$.name }</td>%%</td>
            <td ifcheck="!topics$topics$.countcount == 0" lookup="true"
            <td if="topics$.count == 1" lookup="true">{ topics$.count } comment</td>  No comments yet</td>
            <td ifcheck="topics$.count > == 1" lookup="true">{ topics$.count } comments</td>
                %% topics$.count %% comment</td>
            <td check="topics$.count > 1" lookup="true">
                %% topics$.count %% comments</td>
        <tr ifcheck="!topics"
            <td colspan="2" align="center" lookup="true">This list is empty</td>
                This list is empty</td>


    // function at Site.prototype.controls.list
    var content = {
        main : this.views.list.render()
    var page = Page.views.default.render(content);
    return page;

this.views.list.control() is invoked when the list view is rendered. When it is invoked, "that" is the object which was passed as parameter of the render() method or otherwise an empty/default object. "that" is the object that will be used as the Javascript context when rendering the view.

Views would be E4X Javascript XML objects. When Helma would render these views, it would parse them for "lookup", "if" and "for" attributes and then evaluate them in the context of "that".

When parsing the "for" attributes, Helma would loop through the indicated object (topics) with <nobr>"for(var $topics in topics)"</nobr> and repeat the specified element (the table row) with <nobr>topics$ == topics[$topics]</nobr>

When parsing the "lookup" attributes, Helma would check if a lookup handler function exists and if so would apply it to the element's content.

this.controls.list would be the function that is called in order to handle the request, similar to todays this.list_action.

As a reality check for this proposal I so far came up with a specialized prototype of the rendering method. The idea seems to work quite nicely already with current builds of Helma 2. I thought E4X could make implementing this very easy, but I was surprised that it really turned out that way in practice:

    function handle_get() {
        // first prototype for the next incarnation of mocha objects
        // the render function, providing the magic
        var render = function(view) {
            // loop
            var toLoop = view..*.(@loop.toSource() != '<></>');
            for (var item in toLoop) {
                var dataName = toLoop[item].@loop.toString();
                delete toLoop[item].@loop;
                var data = eval(dataName);
                var stamp = toLoop[item].toString();
                for (var i in data) {
                    var stencil = stamp.replace(
                        eval("/%% "+dataName+"\\$\\.(.+) %%/g"),
                        '{ '+dataName+'['+i+'].$1 }'
                    stencil = stencil.replace(
                    stencil = stencil.replace(
                        eval('/"%% (.*)\\$'+dataName+'(.*) %%"/g'),
                        '{ $1'+i+'$2 }'
                delete toLoop[item].parent().*[toLoop[item].childIndex()];
            // lookup
            var toLookup = view..*.(@lookup.toSource() != '<></>');
            for (var item in toLookup) {
                delete toLookup[item].@lookup;
                var lookedup = '('+toLookup[item].children()[0]+')';
            // check
            var toCheck = view..*.(@check.toSource() != '<></>');
            for (var item in toCheck) {
                if (eval(toCheck[item].@check.toString()))
                    delete toCheck[item].@check;
                    toCheck[item] = '';
            return view;
        // an example skin
        var myview = <table border="1">
            <tr check="topics">
                <th lookup="true">Topic Name</th>
                <th lookup="true">Comment Count</th>
            <tr loop="topics" class="%% 'rowcolor'+ ($topics % 2) %%">
                <td>%% topics$.name %%</td>
                <td check="topics$.count == 0" lookup="true">
                    No comments yet</td>
                <td check="topics$.count == 1" lookup="true">
                    %% topics$.count %% comment</td>
                <td check="topics$.count > 1" lookup="true">
                    %% topics$.count %% comments</td>
            <tr check="!topics">
                <td colspan="2" align="center" lookup="true">
                    This list is empty</td>
        // the example data
        var topics = [
            {name : 'Peter', count : 0},
            {name : 'Wolf', count : 2},
            {name : 'John', count : 1},
            {name : 'Andy', count : 8}
        // rendering the list
        var listtable = render(myview.copy());
        // rendering the empty table
        topics = null;
        var emptytable = render(myview.copy());
        // assembling the page
        var page = <html><body>{ listtable }<hr/>{ emptytable }</body></html>;
        // writing the page source to the reponse buffer
        res.contentType = "text/html";

Resulting in the following output:

        <table border="1">
            <th>(Topic Name)</th>
            <th>(Comment Count)</th>
          <tr class="rowcolor0">
            <td>(No comments yet)</td>
          <tr class="rowcolor1">
            <td>(2 comments)</td>
          <tr class="rowcolor0">
            <td>(1 comment)</td>
          <tr class="rowcolor1">
            <td>(8 comments)</td>
        <table border="1">
            <td colspan="2" align="center">
                (This list is empty)</td>