-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handlebars interoperability #169
Comments
Starting in v0.9.1, you can pass in compiled templates i.e. var source = $("#entry-template").html(),
template = Handlebars.compile(source);
$('.typeahead').typeahead({
// ...
template: template
}); |
@jharding thanks for the fast reply. I ended up using the template function as a workaround, but it still feels hackish (particularly since I have to add the I'd much prefer to do this: $('.typeahead').typeahead
name: 'countries'
template: "{{countryName}} (<strong>{{countryCode}}</strong>)"
engine: Handlebars to this $('.typeahead').typeahead
name: 'countries'
template: (data)->
Handlebars.compile("<div class='tt-suggestion'>{{countryName}} (<strong>{{countryCode}}</strong>)</div>", data) Same deal for underscore, EJS, etc. |
Thanks for mentioning this, I totally forgot about it. This was the reason why I choose not to support compiled templates in the first place, but it slipped my mind when I accepted a pull request for it a couple weeks ago. For now I'm going to update the README to not mention compiled templates and then I'll fix this in the next release. Tracking this in #170.
I didn't want to have to deal with supporting different kinds of templating engines, so I decided the easiest approach would be to support one API and I just so happened to chose Hogan's API since that's what we use. Once the compiled template stuff is figured out, this shouldn't be an issue anymore. |
That seems like a totally reasonable solution. I like Hogan, but since I'm using Handlebars instead on this current project, it just makes things easier if everything works without fuss. What if template could no longer be passed as a string, and the developer was left to implement the specific compile function (thereby eliminating the template = "{{countryName}} (<strong>{{countryCode}}</strong>)"
$('.typeahead').typeahead
name: 'countries'
template: Handlebars.compile(template)
$('.typeahead').typeahead
name: 'countries'
template: Hogan.compile(template).render
$('.typeahead').typeahead
name: 'countries'
template: (data) ->
"#{data.countryName} <strong>(#{data.countryCode})</strong>" |
I wouldn't necessarily be opposed to only supporting compiled templates, but I think some devs find it easier to pass a string template and an engine. Plus if we removed it, it'd break backwards compatibility and I'd prefer to only do that when absolutely necessary. |
I like the idea of passing an imported template and an engine too, but I can't think of any engines off the top of my head that support the Let me whip up a pull really quick. |
FYI I created #170 to track wrapping compiled templates. If you don't submit a pull request for it, I'll take care of it next time I work on typeahead.js. |
I made a first stab in 172. It takes away the flexibility of deciding not to include the tt-suggestion class in your markup (or putting it on a different tag like an |
Handlebars template functions don't have a
render
method, which prevents the engine from working with typeahead templates. I'm pretty happy with Handlebars, and it'd be great if typeahead worked out of the box with it.A possible fix could be to check for the presence of the render method for Hogan, and fallback to
compiledTemplate
for Handlebars/underscore/JST, etc in thecompileTemplate
fn.(or something more elegant, but you get the idea)
The text was updated successfully, but these errors were encountered: