Karatay Karatay - 1 month ago 11
Javascript Question

Handlebars helper is not defined.

I have div that displays a content from mysql database. The content is too long so I decided to use Handlebar helpers. Here is my html code:

<div class="row">
{{#if news}}
{{#each news}}

<div class="col-md-4 img-portfolio">
<a href="/news-details/{{id}}">
<img class="img-responsive img-hover small-img" src="/images/news/{{image}}" alt="">
</a>
<h3>
<a href="/news-details/{{id}}">{{title}}</a>
</h3>
<p> {{substr "10" text}} </p>
</div>
{{/each}}
{{else}}
<p>No News</p>
{{/if}}




The Handlebar Helper is placed in index.js file as follows:

Handlebars.registerHelper('substr', function(length, context, options) {
if ( context.length > length ) {
return context.substring(0, length) + "...";
} else {
return context;
}
});


When I run the nodejs server, I receive "Handlebar is not defined" error. I installed express-handlebar package and also declared it on the index.js file as

var Handlebar = require('express-handlebars');


But it still didn't work.

Answer

Well, this is not a "right" answer but it made the problem to be solved. I used CSS to make it work. Simply I added an ID as "test" for the paragraph tag and assign css in the style.css file as follow:

#test {
-o-text-overflow: ellipsis;   /* Opera */
text-overflow:    ellipsis;   /* IE, Safari (WebKit) */
overflow:hidden;              /* don't show excess chars */
white-space:nowrap;           /* force single line */
width: 350px;                 /* fixed width */
}
Comments