问题
In jinja2(python) template engine there is a simple thing for truncating strings:
{{ fooText|truncate(200) }}
Does meteor(handlebars) provides something like this?
回答1:
I never use |
on spacebars (the engine used on meteor template), but you can do a helper to accomplish this(for example a global Template.registerHelperr).
Template.registerHelper('text', function(passedString) {
var fooText = passedString.substring(0,1); //same as truncate.
return new Spacebars.SafeString(fooText)
});
And use it like {{ text myString}}
Here we are using some Blaze and the substring method.
回答2:
I am using values as options, starting value as well as ending value passed as arguments form template. Try this:
Handlebars.registerHelper('trimString', function(passedString, startstring, endstring) {
var theString = passedString.substring( startstring, endstring );
return new Handlebars.SafeString(theString)
});
In template:
<p>{{{trimString value 0 300}}}</p>
it'll print first 300 characters of the value. Hope this help you.
来源:https://stackoverflow.com/questions/29476505/how-to-truncate-string-using-meteor-and-handlebars