Skip to content

Commit

Permalink
make mustache streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
janl committed Feb 3, 2010
1 parent ec18e0e commit d0d46e5
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 48 deletions.
1 change: 0 additions & 1 deletion examples/array_of_strings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

hello world
1 change: 0 additions & 1 deletion examples/comments.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<h1>A Comedy of Errors{{! just something interesting... or not... }}</h1>

1 change: 0 additions & 1 deletion examples/complex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
</ul>

3 changes: 2 additions & 1 deletion examples/delimiters.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{=<% %>=}}* <% first %>
{{=<% %>=}}*
<% first %>
* <% second %>
<%=| |=%>
* | third |
Expand Down
5 changes: 2 additions & 3 deletions examples/delimiters.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
* It worked the first time.
*
It worked the first time.
* And it worked the second time.

* Then, surprisingly, it worked the third time.

* Fourth time also fine!.
1 change: 0 additions & 1 deletion examples/pragma.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@


2 changes: 0 additions & 2 deletions examples/recursion_with_same_names.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name
desc

t1
0
t2
1

1 change: 0 additions & 1 deletion examples/reuse_of_enumerables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ t1
0
t2
1

1 change: 0 additions & 1 deletion examples/simple.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Hello Chris
You have just won $10000!
Well, $6000, after taxes.

93 changes: 57 additions & 36 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@ var Mustache = function() {
otag: "{{",
ctag: "}}",
pragmas: {},
buffer: [],

render: function(template, context, partials) {
render: function(template, context, partials, in_recursion) {
// fail fast
if(template.indexOf(this.otag) == -1) {
return template;
}

if(!in_recursion) {
this.buffer = [];
}

template = this.render_pragmas(template);
var html = this.render_section(template, context, partials);
return this.render_tags(html, context, partials);
if(in_recursion) {
return this.render_tags(html, context, partials, in_recursion);
}

this.render_tags(html, context, partials, in_recursion);
},

/*
Sends parsed lines
*/
send: function(line) {
if(line != "") {
this.buffer.push(line);
}
},

/*
Expand Down Expand Up @@ -55,7 +73,7 @@ var Mustache = function() {
if(!partials || !partials[name]) {
throw({message: "unknown_partial"});
}
return this.render(partials[name], context[name], partials);
return this.render(partials[name], context[name], partials, true);
},

/*
Expand All @@ -76,10 +94,10 @@ var Mustache = function() {
if(that.is_array(value)) { // Enumerable, Let's loop!
return that.map(value, function(row) {
return that.render(content, that.merge(context,
that.create_context(row)), partials);
that.create_context(row)), partials, true);
}).join('');
} else if(value) { // boolean section
return that.render(content, context, partials);
return that.render(content, context, partials, true);
} else {
return "";
}
Expand All @@ -89,41 +107,39 @@ var Mustache = function() {
/*
Replace {{foo}} and friends with values from our view
*/
render_tags: function(template, context, partials) {
var lines = template.split("\n");
render_tags: function(template, context, partials, in_recursion) {
// tit for tat
var that = this;

var new_regex = function() {
return new RegExp(that.otag + "(=|!|>|\\{|%)?([^\/#]+?)\\1?" +
that.ctag + "+", "g");
};

// tit for tat
var that = this;

var regex = new_regex();
for (var i=0; i < lines.length; i++) {
lines[i] = lines[i].replace(regex, function (match,operator,name) {
switch(operator) {
case "!": // ignore comments
return match;
case "=": // set new delimiters, rebuild the replace regexp
that.set_delimiters(name);
regex = new_regex();
// redo the line in order to get tags with the new delimiters
// on the same line
i--;
return "";
case ">": // render partial
return that.render_partial(name, context, partials);
case "{": // the triple mustache is unescaped
return that.find(name, context);
return "";
default: // escape the value
return that.escape(that.find(name, context));
}
},this);
};
return lines.join("\n");
var lines = template.split("\n");
for (var i=0; i < lines.length; i++) {
lines[i] = lines[i].replace(regex, function(match, operator, name) {
switch(operator) {
case "!": // ignore comments
return match;
case "=": // set new delimiters, rebuild the replace regexp
that.set_delimiters(name);
regex = new_regex();
return "";
case ">": // render partial
return that.render_partial(name, context, partials);
case "{": // the triple mustache is unescaped
return that.find(name, context);
default: // escape the value
return that.escape(that.find(name, context));
}
}, this);
if(!in_recursion) {
this.send(lines[i]);
}
}
return lines.join("\n");
},

set_delimiters: function(delimiters) {
Expand Down Expand Up @@ -249,13 +265,18 @@ var Mustache = function() {

return({
name: "mustache.js",
version: "0.2",
version: "0.3a",

/*
Turns a template and view into HTML
*/
to_html: function(template, view, partials) {
return new Renderer().render(template, view, partials);
to_html: function(template, view, partials, send_fun) {
var renderer = new Renderer();
if(send_fun) {
renderer.send = send_fun;
}
renderer.render(template, view, partials);
return renderer.buffer.join("\n");
}
});
}();

0 comments on commit d0d46e5

Please sign in to comment.