How to truncate string using meteor and handlebars?

一世执手 提交于 2020-01-03 02:02:05

问题


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

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