Search code examples
javascriptcharacterejslimit

Set character limit in ejs


I am fetching content from JS in EJS template. I have a body field that contains more than 200 characters, but I want to show only 100 Characters in my view.


Solution

  • Simply you can put up an if else condition like this in your EJS Template:

    <%if (yourField.length > 100) { %>
    // Code to show 100 characters
    <%= yourField.substring(0, 99) %>
    <% } %>
    <% else{ %>
    //COde to print full value of your field
    <%= yourField %>
    <% } %>