Helma Logo
main list history
previous version  overview  next version

Version 7 by michi on 07. December 2005, 11:18

NOTYETFINISHED

* follow february-proposal, starting with andreas <a href="http://helma.org/pipermail/helma-dev/2005-February/001714.html">code snippet</a> from back then
* allow walking through object-structure (not really necessary, since this could be implemented by passing along a parameter to the macro, but a 'this.topics.count' is definitely prefered to 'this.count collection="topics"'
* either use <code><% .. %></code> or <h:macro .. />, but dont allow both
* XHTML conformance of the templates is an absolut non-goal for me. i just dont see a reason for that. Additionally macros are inserted not just outside of tags, but also within e.g. a style-attribute of a tag. The <h:macro .. />-syntax there breaks xml-conformance anyways.
* <h:macro call="param.i" /> just seems to be an overkill for what has been <code><% param.i %></code> before
* I consider pushing information to skins via the param-object resp response-object not desirable, since these parameters are hard to document for template designers.
* keep it simple, and avoid magic, non-intuitive behavior

[Still thinking; My current code snippets will follow]snippet:

  # HopObject/macros.js
  function count_macro(param) {
    var cnt = this.count();
    if (param[cnt.toString()]) {
      res.write(param[cnt.toString()]);
    } else if (param["n"]) {
      res.write(param["n"]);
    } else {
      res.write(cnt);
    }
  }
  function loop_macro(param) {
    if (param["default"]) param["default"] = this.renderSkinAsString(createSkin(param["default"]));
    if (param["prefix"]) param["prefix"] = this.renderSkinAsString(createSkin(param["prefix"]));
    if (param["suffix"]) param["suffix"] = this.renderSkinAsString(createSkin(param["suffix"]));
    for (var i=0; i<this.count(); i++) {
      var obj = this.get(i);
      var skinParam = {};
      skinParam.cnt = i;
      if (param.alternate) skinParam.alternateCnt = i % param.alternate;
      if (param["body"]) {
        obj.renderSkinAsString(createSkin(param[renderSkin(createSkin(param["body"]), skinParam);
      } else {
        // render separate skin
        obj.renderSkin(param["skin"] || "listitem", skinParam);
      }
    }
  }
  function msg_macro(param) {
    renderSkin(createSkin(getText(param.id)));
  }
  # Global/functions.js
  function getText(id) {
    ..
    return str;
  }

  <h1><hc:this.msg id="site.topics.header">All topics</hc:this.msg></h1>
  <p><hc:this.topics.count>
    <ha:0><hc:this.msg id="site.topics.noTopics">No topics to display.</hc:this.msg></ha:0>
    <ha:1><hc:this.msg id="site.topics.oneTopic">This site has just one topic.</hc:this.msg></ha:1>
    <ha:n><hc:this.msg id="site.topics.moreTopics">This site has <hc:this.count /> topics.</hc:this.msg></ha:n>
  </hc:this.topics.count></p>
  <hc:site.loop alternate="2">
    <ha:prefix>
    <table>
      <tr>
        <th><hc:this.msg id="site.topics.tableHead.topic">Topic</hc:this.msg></th>
        <th><hc:this.msg id="site.topics.tableHead.numberOfStories">Number of Stories</hc:this.msg></th>
      </tr>
    </ha:prefix>
    <ha:body>
      <tr class="row<hc:param.alternateCnt />">
        <td><hc:topic.name /></td>
        <td><hc:topic.count /></td>
      </tr>
    </ha:body>
    <ha:suffix>
    </table>
    </h:suffix>
  </hc:site.loop>

<b>Explanation</b>: What has been before <code><% this.hello key1="value01" %></code>, is now <code><hc:this.hello key1="value01" /></code>, which is nothing but a transformation of syntax. The difference now is, that additional parameters to the macro can be specified by embedding ha-tags into the hc-tag.

Note: Lots of this.renderSkin(createSkin(.)) are needed in the above example, so an idea would be that Helma does that automatically for each 'ha'-macro argument, but then i wouldn't know how to push additional parameters to these strings/subskins.

     removed
     added