Permalink
Browse files

Added if/then/else transform

  • Loading branch information...
1 parent 1538b54 commit 214f5b756fd6565b30d6ac8a132e939ca5d83f9d @xavi- committed Jun 9, 2011
Showing with 46 additions and 0 deletions.
  1. +12 −0 README.markdown
  2. +21 −0 index.js
  3. +13 −0 test/test2.js
View
@@ -282,6 +282,18 @@ This is useful when you want to reuse a template on the client side and on the s
</table>" */
});
+### Making if/then/else statement
+####The Markup:
+ <div>(: if[signed-in] ~
+ [: then ~ Welcome user :]
+ [: else ~ Sign in here :]:)
+ </div>
+####The Code:
+ var bind = require("bind");
+
+ bind.toFile("./file.html",{ "signed-in": true },
+ function callback(data) { /* data === "<div>Welcome user</div>" */ });
+
## Fine points
### `null` vs `undefined`
View
@@ -76,6 +76,10 @@
if(val == undefined) { val = predefines[key]; }
if(anchors.isAnchor(key)) { anchors.append(key, defVal); callback(""); return; }
+ if(/(.+?)\[(.+?)\]/.test(key)) { // is Transform
+ var match = /(.+?)\[(.+?)\]/.exec(key);
+ val = transforms[match[1]](match[2]);
+ }
if(val == undefined) { callback(defVal); return; }
if(toString.call(val) === "[object Number]") { callback(val.toString()); return; }
@@ -175,6 +179,23 @@
}
};
+ var transforms = {
+ "if": function(key) {
+ return function(callback, defVal, context) {
+ bind.to(
+ levelUp(defVal), {
+ "then": function(callback, defVal) {
+ if(context[key]) { callback(defVal); } else { callback(""); }
+ },
+ "else": function(callback, defVal) {
+ if(context[key]) { callback(""); } else { callback(defVal); }
+ }
+ }, callback
+ );
+ };
+ }
+ };
+
function toFile(path, context, callback) {
retrieveFile(path, function(data) { bind.to(data, context, callback); });
}
View
@@ -161,6 +161,19 @@ bind.to(
function callback(data) { assert.equal(data, "<div></div><div>poo</div>"); }
);
+
+bind.to(
+ "<div>(: if[signed-in] ~ [: then ~ Welcome user :][: else ~ sign in here :]:)</div>",
+ { "signed-in": true },
+ function callback(data) { assert.equal(data, "<div>Welcome user</div>"); }
+);
+
+bind.to(
+ "<div>(: if[signed-in] ~ [: then ~ Welcome user :][: else ~ sign in here :]:)</div>",
+ { "signed-in": false },
+ function callback(data) { assert.equal(data, "<div>sign in here</div>"); }
+);
+
/*
### Embed files:
####The Markup:

0 comments on commit 214f5b7

Please sign in to comment.