leshill / handlebars_assets

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

Template names include 'javascripts/templates' #40

Closed stravid closed 11 years ago

stravid commented 11 years ago

Hi!

The readme states the following:

Add the template code to the HandlebarsTemplates global under the name contacts/new

But in my little project this is not the case. Maybe I do something wrong, I would really appreciate if you could take a look.

I have a simple config.ru:

require 'sprockets'
require 'handlebars_assets'

project_root = File.expand_path(File.dirname(__FILE__))
assets = Sprockets::Environment.new(project_root) do |env|
  env.logger = Logger.new(STDOUT)
end

HandlebarsAssets::Config.template_namespace = 'Ember.TEMPLATES'

assets.append_path(File.join(project_root, 'app', 'assets'))
assets.append_path(File.join(project_root, 'app', 'assets', 'javascripts'))
assets.append_path(File.join(project_root, 'app', 'assets', 'stylesheets'))

assets.append_path(File.join(project_root, 'vendor', 'assets'))
assets.append_path(File.join(project_root, 'vendor', 'assets', 'javascripts'))
assets.append_path(File.join(project_root, 'vendor', 'assets', 'stylesheets'))

assets.append_path HandlebarsAssets.path

map "/assets" do
  run assets
end

map "/" do
  run lambda { |env|
    [
      200,
      {
        'Content-Type'  => 'text/html',
        'Cache-Control' => 'public, max-age=86400'
      },
      File.open('public/index.html', File::RDONLY)
    ]
  }
end

When I require all my templates in my application.js.coffee with #= require_tree ./templates I get the following output:

this.Ember.TEMPLATES["javascripts/templates/application"] = ...

I would love to remove the "javascripts/templates" part from the name, is this possible?

Thanks for your time and awesome gem!

leshill commented 11 years ago

Hi @stravid,

Without actually trying it, this is odd to me (and duplicated -- typo?):

assets.append_path(File.join(project_root, 'app', 'assets'))

Do you really need that? It may be that the Rails sprocket gem adjusts this. Try it and let me know if that changed it. If not, I will take a look when I have some time later today?

stravid commented 11 years ago

Good catch! Unfortunately nothing changed. I would really appreciate it, if you find some time later in the day :-)

leshill commented 11 years ago

Hi @stravid,

I created a simple app that does the right thing from your code. The only difference that I can see is that I removed this line:

assets.append_path(File.join(project_root, 'app', 'assets'))

The app is just_rack. Pull it down, setup your rvm gemset, bundle, and start it:

rackup

Then point your browser to localhost:9292/assets/application.js and this is what I see:

      (function() {
        this.Ember.TEMPLATES || (this.Ember.TEMPLATES = {});
        this.Ember.TEMPLATES["one"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
        helpers = helpers || Handlebars.helpers;
        var buffer = "", stack1, foundHelper, functionType="function", escapeExpression=this.escapeExpression;

        foundHelper = helpers.token;
        if (foundHelper) { stack1 = foundHelper.call(depth0, {hash:{}}); }
        else { stack1 = depth0.token; stack1 = typeof stack1 === functionType ? stack1() : stack1; }
        buffer += escapeExpression(stack1) + "\n";
        return buffer;});
        return this.Ember.TEMPLATES["one"];
      }).call(this);

That seems to be what you want. Does that help?

stravid commented 11 years ago

Thanks a lot, I owe you a drink!

This solved the problem :-)

leshill commented 11 years ago

Awesome! Find me at a conference :)