Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content truncation which ends with "..." #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 41 additions & 12 deletions mustache.js
Expand Up @@ -155,24 +155,53 @@ var Mustache = function() {


var regex = new_regex(); var regex = new_regex();
var tag_replace_callback = function(match, operator, name) { var tag_replace_callback = function(match, operator, name) {

var truncate = {
identifier: ':',
shouldI: false,
after: null
};

if (name.indexOf(truncate.identifier) !== -1) {
truncate.after = name.split(truncate.identifier)[1];
truncate.shouldI = true;

name = name.replace(truncate.identifier, '');
name = name.replace(truncate.after, '');
}

var found = null;

switch(operator) { switch(operator) {
case "!": // ignore comments case "!": // ignore comments
return ""; found = "";
case "=": // set new delimiters, rebuild the replace regexp break;
that.set_delimiters(name); case "=": // set new delimiters, rebuild the replace regexp
regex = new_regex(); that.set_delimiters(name);
return ""; regex = new_regex();
case ">": // render partial found = "";
return that.render_partial(name, context, partials); break;
case "{": // the triple mustache is unescaped case ">": // render partial
return that.find(name, context); found = that.render_partial(name, context, partials);
default: // escape the value break;
return that.escape(that.find(name, context)); case "{": // the triple mustache is unescaped
found = that.find(name, context);
default: // escape the value
found = that.escape(that.find(name, context));
break;
} }

if (truncate.shouldI && found && found.length > truncate.after) {
found = found.substring(0, truncate.after) + ' ...';
}

return found;
}; };

var lines = template.split("\n"); var lines = template.split("\n");
for(var i = 0; i < lines.length; i++) { for(var i = 0; i < lines.length; i++) {
lines[i] = lines[i].replace(regex, tag_replace_callback, this); lines[i] = lines[i].replace(regex, tag_replace_callback, this);

if(!in_recursion) { if(!in_recursion) {
this.send(lines[i]); this.send(lines[i]);
} }
Expand Down