Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
wants to merge 2 commits into from

4 participants

André König Jan Lehnardt Rich Linstead Justin Hileman
André König

In my current project it was necessary for me to truncate the strings within a template to a specific length and add the omnipresent "...".

I thought it would be cool if there is a notation for mustache tags to handle this problem. So I've implemented a small modification of the "render_tags" method. With this it is possible to shorten the string by defining the length within the tag:

{{thetag:20}} -> Shorten the "thetag" content to 20 chars and end up with " ...".

So maybe this is useful for somebody.

Regards,
André

Jan Lehnardt
Owner

I'd rather see helpers implemented rather than adding syntax for all these things. See http://writing.jan.io/mustache-2.0.html

André König

Well :)

Jan Lehnardt
Owner

Could be an extra too :) Either way, this would have to be rebased against master/0.5.0-dev

Rich Linstead

It's a lot easier to do it with handlebars.js and a custom helper function:

<script id="entry-template" type="text/x-handlebars-template">
    <div class="entry">
        <h1>{{title}}</h1>
        <div class="body">
            {{truncate body max=10}}
        </div>
    </div>
</script>

<script type="text/javascript">
    Handlebars.registerHelper('truncate', function(val, options){ 
        var max = options.hash.max;

        return val.length > max ? val.substring(0,max) : val;
    });

    var source   = $("#entry-template").html();
    var template = Handlebars.compile(source);
    var context = {title: "My New Post", body: "Thisismynewpost"}
    var html    = template(context);
    console.log(html);
</script>
Justin Hileman

@FiveCornerSquare Yes, but then your template is no longer logic-less :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 7, 2011
  1. André König
Commits on Aug 11, 2011
  1. André König
This page is out of date. Refresh to see the latest.
Showing with 41 additions and 12 deletions.
  1. +41 −12 mustache.js
53 mustache.js
View
@@ -155,24 +155,53 @@ var Mustache = function() {
var regex = new_regex();
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) {
- case "!": // ignore comments
- return "";
- 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));
+ case "!": // ignore comments
+ found = "";
+ break;
+ case "=": // set new delimiters, rebuild the replace regexp
+ that.set_delimiters(name);
+ regex = new_regex();
+ found = "";
+ break;
+ case ">": // render partial
+ found = that.render_partial(name, context, partials);
+ break;
+ 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");
for(var i = 0; i < lines.length; i++) {
lines[i] = lines[i].replace(regex, tag_replace_callback, this);
+
if(!in_recursion) {
this.send(lines[i]);
}
Something went wrong with that request. Please try again.