leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
648 stars 159 forks source link

HandlebarsTemplates is not defined #82

Closed kwhitaker closed 11 years ago

kwhitaker commented 11 years ago

Trying to get this gem working in our Rails 3 app, but the global namespace for templates is coming back as 'undefined,' both in my coffee files and in my Chrome console.

My application.js.coffee looks like this:

#= require './lib/jquery.min'
#= require './lib/underscore.min'
#= require './lib/backbone.min'
#= require './lib/marionette.min'
#= require 'handlebars'
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require 'connectors.app'

I've tried moving the ./templates directory up and down the list, and nothing fixes it. Everything else functions just fine (I'm able to call Backbone.Model, for instance).

This is in my dev environment. My development.rb looks like this:

Connectors::Application.configure do
  config.cache_classes = false
  config.whiny_nils = true
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.action_dispatch.best_standards_support = :builtin
  config.active_record.mass_assignment_sanitizer = :strict
  config.active_record.auto_explain_threshold_in_seconds = 0.5
  config.assets.compress = false
  config.assets.debug = true
end

Thanks for any help!

kwhitaker commented 11 years ago

Nevermind, hah. Was a naming problem. My templates were named .html.slimbars, as opposed to just .slimbars.

AlexRiedler commented 11 years ago

^ reasons I wish the asset and view pipelines in rails were merged together.

wideopenspaces commented 10 years ago

Is there any way to make using .html.hbs work? I am using the same template for both client and server-side rendering, and the solution I came up with requires it to be named {action}.html.hbs ..HBA compiles it just fine, but neglects to ever include the compiled file.

If I copy and paste the compiled template into the console, everything's hunky-dory.

If I can't figure out the extension piece... well, I'll have to figure out how to stop Rails from rendering my JSON response with the handlebars template :/

AlexRiedler commented 10 years ago

@wideopenspaces, What do you mean by it "neglects to ever include the compiled file" ?

wideopenspaces commented 10 years ago

I think 'require_tree' skips it, because the compiled file ends up with the name 'show.html' ... if I can figure out a way to strip all extensions instead of just the last, I imagine it would be written out to 'show.js' and be properly included.

AlexRiedler commented 10 years ago

@wideopenspaces; make handlebars register the combined extension might work as in ".html.hbs" instead of the default. If that doesn't work I can see what I can do.

wideopenspaces commented 10 years ago

Yep, I tried that one. I'll poke around a bit more to see what I can figure out.

AlexRiedler commented 10 years ago

@wideopenspaces are you on the latest version that came out this weekend? make sure to clear your rails cache when testing (I know,.... its really annoying to test).

AlexRiedler commented 10 years ago

Also what gem are you using to do "same template for both client and server-side rendering" ?

wideopenspaces commented 10 years ago

I was working on my fork of @variousauthor's fork with support for server-side rendering, attempting to make it work cleanly with render_with ... I got it working (as an aside, I lost an entire weekend to the innards of ActionView and ActionController mysteries): https://github.com/wideopenspaces/handlebars_assets/compare/adds_responder

When I came up for air, you'd released the brand new version. I was going to attempt to get things working on the old version, then pull the new version and see if I could port them over.

AlexRiedler commented 10 years ago

Oh Okay, @wideopenspaces been there done that see: https://github.com/AlexRiedler/tilt_action_view

wideopenspaces commented 10 years ago

I wandered through Sprockets. The answer was staring me in the face in the code.

If I name the template show.jst.hbs, Sprockets saves the compiled template as show.jst, which it is more than happy to include. I just had to adjust my code to look for formats: [:js, :jst] instead of formats: [:html], and remove jst.hbs from the list of extensions that get the custom ActionView renderer.

(The reason for the last part is this: if jst.hbs is registered to a custom renderer in ActionView, the .jst part of the extension is not seen as a format extension)

AlexRiedler commented 10 years ago

@wideopenspaces good job, now I just need to document it ...; the only major code change in the new version fyi is a file rename (since the old name is not consistent with other ruby tilt libraries).

wideopenspaces commented 10 years ago

I'm pretty sure the one I forked from is at least a few generations back, but that won't stop me!