Shuo/client/js/handlebars.helpers.js

38 lines
799 B
JavaScript
Raw Normal View History

2014-05-02 17:46:18 +00:00
Handlebars.registerHelper(
"slice", function(items, block) {
var limit = block.hash.limit;
var rows = [];
items.forEach(function(i) {
rows.push(block.fn(i));
});
var html = "";
2014-05-02 21:54:31 +00:00
var hidden = rows
2014-05-02 17:46:18 +00:00
.slice(0, Math.max(0, rows.length - limit))
.join("");
2014-05-02 21:54:31 +00:00
if (hidden != "") {
html = "<script type='text/html' class='hidden'>" + hidden + "</script>";
2014-05-02 17:46:18 +00:00
}
html += rows.slice(-limit).join("");
return html;
}
);
function escape(text) {
var e = {
"<": "&lt;",
">": "&gt;"
};
return text.replace(/[<>]/g, function (c) {
return e[c];
});
}
Handlebars.registerHelper(
"uri", function(text) {
text = escape(text);
return URI.withinString(text, function(url) {
return "<a href='" + url.replace(/^www/, "//www") + "' target='_blank'>" + url + "</a>";
});
}
);