How to truncate string using meteor and handlebars?

一个人想着一个人 提交于 2019-12-09 00:59:27

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!