Permalink
Browse files

support for string templates

  • Loading branch information...
1 parent cb5813c commit 3c5d323bdd15eb8c07258e64bf223934da804a0f @adamstallard committed Sep 4, 2013
View
@@ -251,41 +251,65 @@ this.modest = this.modest || {
$targets.each(function(){
var $target = $(this);
- var uses = $target.attr('uses').toLowerCase().split(' ');
- var eq, param, u, attr;
+ var eq, param, u, attr, strTemplates;
+ var usesString = $target.attr('uses');
+
+ // string template substitution
+
+ usesString = usesString.replace(/{{([^{}]+)}}/g, function(match, param){
+ strTemplates = true;
+ return params[param.toLowerCase()] || '';
+ }
+ );
+
+ // temporarily replace spaces in string template parameters so they don't get split
+
+ if(strTemplates){
+ usesString = usesString.replace(/\s(?=(?:[^{]|{(?=[^{]))*}})/g, 'mSoPdAeCsEt');
+ }
+
+ var uses = usesString.split(' ');
for (u = 0; u < uses.length; ++u) {
switch (uses[u][0]) {
case '+':
- if (params[uses[u].slice(1)] === undefined) {
+ if (params[uses[u].slice(1).toLowerCase()] === undefined) {
$target.remove();
break;
}
break;
case '-':
- if (params[uses[u].slice(1)] !== undefined) {
+ if (params[uses[u].slice(1).toLowerCase()] !== undefined) {
$target.remove();
break;
}
break;
+ case '{':
+ $target.html(modest.stripTemplate(uses[u]));
+ break;
default:
eq = uses[u].indexOf('=');
if (eq !== -1) {
attr = uses[u].slice(0, eq);
param = uses[u].slice(eq + 1);
- if (params[param]) {
- $target.attr(attr, params[param]);
+ var pVal = params[param.toLowerCase()];
+ if (pVal) {
+ $target.attr(attr, pVal);
+ }
+ else if(param[0] == '{'){
+ $target.attr(attr, modest.stripTemplate(param));
}
}
else {
- if (params[uses[u]] === undefined) {
+ param = uses[u].toLowerCase();
+ if (params[param] === undefined) {
$target.remove();
break;
}
else {
- $target.attr(paramAttrs[uses[u]] || {});
- $target.html(params[uses[u]]);
- $target.addClass(uses[u]);
+ $target.attr(paramAttrs[param] || {});
+ $target.html(params[param]);
+ $target.addClass(param);
}
}
}
@@ -294,9 +318,19 @@ this.modest = this.modest || {
return $view;
}.bind(this),
- render : function(module, parameters){
+ stripTemplate : function(s){
+
+ // remove {{}}
+
+ s = s.slice(2, s.length - 2);
+
+ // put back the spaces we replaced earlier
+
+ return s.replace('mSoPdAeCsEt', ' ');
+ },
+ render : function(module, params){
var $view = $('<div>');
- $view = this.modest.compileView($view, module, parameters);
+ $view = this.modest.compileView($view, module, params);
$view.find('[uses]').not('[uses=""]').removeAttr('uses');
return $view[0].outerHTML;
}.bind(this)
View
@@ -1,6 +1,6 @@
{
"name" : "modest",
- "version" : "2.0.0",
+ "version" : "2.1.0",
"description" : "Create HTML modules (building blocks) that look like new HTML tags. Use modules to form other modules. Easily pass parameters from multiple sources to modules and inner modules. Use as a drop-in templating language to any web framework.",
"keywords" : ["xhtml","html","semantic","template","templating","preview","modular","tags","modules","browser","web"],
"author" : {
@@ -0,0 +1 @@
+<button type="button" uses="onclick={{alert('Hello, {{name}}')}}">Greet</button>
@@ -0,0 +1 @@
+<p uses="{{Hello, {{name}}}}" />
@@ -0,0 +1,7 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ </head>
+ <body>
+ <button type="button" class="greetButton" onclick="alert('Hello, Bob')">Greet</button>
+ </body>
+</html>
@@ -0,0 +1,11 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <include path="modules">greetButton</include>
+ </head>
+ <body>
+ <greetButton>
+ <name>Bob</name>
+ </greetButton>
+ </body>
+</html>
@@ -0,0 +1 @@
+string template in an attribute
@@ -0,0 +1 @@
+<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><button type="button" class="greetButton" onclick="alert('Hello, Bob')">Greet</button></body></html>
@@ -0,0 +1,7 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ </head>
+ <body>
+ <p class="greeting">Hello, Bob</p>
+ </body>
+</html>
@@ -0,0 +1,11 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <include path="modules">greeting</include>
+ </head>
+ <body>
+ <greeting>
+ <name>Bob</name>
+ </greeting>
+ </body>
+</html>
@@ -0,0 +1 @@
+string template in an element
@@ -0,0 +1 @@
+<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><p class="greeting">Hello, Bob</p></body></html>

0 comments on commit 3c5d323

Please sign in to comment.