Search code examples
handlebars.jsjinja2

Get first character from string in handlebars.js


I am new to handlebars and trying to get the first letter from a looped string i.e

{{#each message}}
 <div class='name'>{{this.user}}</div>
 <div class='img'><h1 class="abbr">{{this.user.charAt(0)}}</h1></div>
 <div class='text'>
 <div>
 {{this.text}}
   </div>
   </div>
  {{/each}}

I have tried {{this.text[0]}} and {{this.text.charAt(0)}}


Solution

  • You can use Preparation-Script to achieve this : HandleBars Documents

    must register helper and use it:

    Handlebars.registerHelper('returnOnlyZeroindex', function () {
       return this.user[0]
    })
    

    and use like this:

    <p>{{returnOnlyZeroindex}}</p>