Helma Logo
main list history
previous version  overview  next version

Version 7 by tobi on 20. July 2006, 14:08

var renderSnippet =Just for a simple test implementation I've created two files as attached to this wiki page. function(name, param) (Could be that there's a markup to link them here in the text; alas I don't know it.
var cache;
if (!(cache = res.data["herpes:snippets"])) {
cache = res.data["herpes:snippets"] = new Object;
}
  if (cache[name]) {
  renderSkin(cache[name], param);
  return;
  }

  var NAME = "renderSnippet takes two arguments just as renderSkin does: the first defines the snippet", the second the usual param object.
  var DELIMITER = ":";
  var EXTENSION = "." + NAME + "s";
  var PREFIX = "<!" + NAME + DELIMITER;

  var dir = new File(appThe snippet argument is composed of the file name (without the extension -- which is <code>.dirsnippets</code>, "Global");btw.) and the snippet's name. Both values are separated by a colon.
  var file, snippetName;
  if (name.contains(DELIMITER)) {
      var parts = name.split(DELIMITER);
      file = new File(dir, parts[0] + EXTENSION);
      snippetName = parts[1];
  } else {
      file = new File(dir, name + EXTENSION);
  }

  if (file.exists()) Here's an example call:
      var content = file.readAll();
      var needle, offset, start;
      if (snippetName) {
        needle = PREFIX + snippetName;
        offset = content.indexOf(needle);
        start = offset + needle.length + 1;
      } else {
        offset = content.indexOf(PREFIX);
        start = offset + content.indexOf(">") + 1;
      }
      var end = content.indexOf(PREFIX, start);
      if (end < 0) {
      end = content.length;
      }
      var snippet = content.substring(start, end);
      if (snippet) {
      cache[name] = createSkin(snippet.trim());
        renderSkin(cache[name], param);
      }
  }
  return;
};</code></pre>

<code>renderSnippet("html:image", {src: "http://dev.helma.org/static/xmlbutton.gif"});</code>

This would (or should) render the "image" snippet which is contained in the file <code>Global/html.snippets</code (currently only global snippets are supported).

A snippets file contains HTML markup with Helma macros as we know it. But furthermore, a special tag (which is an HTML comment in disguise and its syntax is still not carved in stone) marks the beginning of a snippet:

<code><!snippet:foobar></code>

Each snippet ends right before the next one begins -- or at the end of the file, resp.

Well, it's all done very quickly and without a lot of error testing or checking, but IMHO it proves the point of subskins pretty well.

     removed
     added