Reputation: 8587
We're starting to use Handlebars for the view layer in some of our projects. We are starting to hit a crossroads between two ways of doing some templating. I've been using partials to handle the templating & having small HTML templates like:
<p id="{{name}}">
<label for="{{name}}Input">{{text}}</label>
{{#if info}}
<small>{{info}}</small>
{{/if}}
<textarea name="{{name}}" id="{{name}}Input"></textarea>
</p>
Another developer feels that we shouldn't be using partials for this & instead we should be creating helpers for this.
I can see helpers being easier to handle input parameters (as I'm currently using some form of "include" helper to include these partials with some extra variables). But it doesn't sit right with me that you are writing HTML into strings in code - I don't think you're separating your concerns properly there. We are also using Handlebars in Java (via [Handlebars.Java][2]), so again your HTML is very much in compiled code - not in simple to edit view files.
Is there a generally accepted way to handle templating in Handlebars? Partials or Helpers or is there something else I don't know about?
Upvotes: 1
Views: 1095
Reputation: 1531
Well, first you need to understand that partials are very different in handlebars.js opposed to handlebars.java
In handlebars.js, you declare your own partials and call/name them whatever you want in your controller (usually) and then call them within your view. In handlebars.java a partial is defined in your view and is essentially just an include taking a path attribute. Includes are generally integral to most projects and I don't think there's any benefit to dropping such an important piece of functionality.
Also, I've seen the mentality of "use helpers for everything" in many handlebars.js apps and it is becomes difficult to maintain very quickly. Helpers are a great feature but they should be used sparingly. Whenever possible, use the built in helpers and try structuring your data in a way that you doesn't require additional abstracted logic.
Looking at your example, i think thats exactly the correct useage of partial.
Upvotes: 0