leshill / handlebars_assets

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

Problem with template path since v0.17 #104

Open ggarnier opened 10 years ago

ggarnier commented 10 years ago

I have a Rails project using handlebars_assets and a personal gem that defines some Handlebars templates. One of the templates in the gem is _app/assets/javascripts/my-gem/component/templates/templatename.hbs. Until handlebars_assets 0.16, this template key in the template_namespace was my-gem/component/templates/template_name. But since 0.17, the key is just template_name.

I took a look at handlebars_assets code, and it seems that TemplatePath class constructor got a scope parameter, and now it gets a _fullpath parameter. This explains the behavior change, as I use the default _pathprefix value (which is templates).

AlexRiedler commented 10 years ago

@ggarnier yeah, this is one of the changes I had to implement to keep consistency across the board; Otherwise through different versions of rails and sprocket engines the results were different.

The workaround would be to change as necessary (unless I am missing something).

ggarnier commented 10 years ago

@AlexRiedler is it possible to set scope on TemplatePath constructor as before (https://github.com/leshill/handlebars_assets/blob/v0.16/lib/handlebars_assets/tilt_handlebars.rb#L91)? I think this could solve the problem.

attenzione commented 10 years ago

i have two template files

/assets/javascripts/builder/templates/nodes/edit.hrb.erb
/assets/javascripts/builder/templates/nodes/show.hrb

before version 0.17 both template has the same namespace, but after edit has nodes/edit, but show - builder/templates/nodes/show

AlexRiedler commented 10 years ago

@ggarnier I am trying to avoid excess work in the slim + haml in production... so using the previous method is not a clear solution; working on a better solution now.

AlexRiedler commented 10 years ago

@attenzione that is super weird, it should be nodes/edit and nodes/show ... what version of rails? (I tested on 4.0)

attenzione commented 10 years ago

@AlexRiedler i use latest 4.1.4, but all time expect builder/templates/nodes/edit, not nodes/edit. It's how it's worked before 0.17 and think should be. If i wanted nodes/edit, i would placed it to /assets/javascripts/templates/nodes/edit

AlexRiedler commented 10 years ago

@attenzione ah, okay that makes more sense. I was actually fixing a bug that had to deal with paths; the new version is very... loosely defined.

If you put this in an initializer this should fix your issue:

::HandlebarsAssets.configure do |config|
   config.path_prefix = 'assets'
end

anything after assets in the path will be then grabbed as the template name.

AlexRiedler commented 10 years ago

@ggarnier actually does ^ also work as a solution also work for you as well ? or not really cause of how the gem is bundled ? (noting that you can write regex's as the path prefix)

attenzione commented 10 years ago

@AlexRiedler very nice, it's resolved my problem! thanx but another problem i think exist, when one template with *.erb extension has different namespace with template without

AlexRiedler commented 10 years ago

@attenzione I think that is mentioned in another ticket; I closed it because it actually is a by-product of the asset compilation pipeline (it does not allow two templates to have the same logical path).

attenzione commented 10 years ago

no, they're not the same logical path, see

/assets/javascripts/builder/templates/nodes/edit.hrb.erb
/assets/javascripts/builder/templates/nodes/show.hrb

and their namespaces should be

builder/templates/nodes/edit
builder/templates/nodes/show

but edit.hbs.erb has only nodes/edit, so the difference is only in extension, if i remove extension .erb, edit template will have the same namespace as show

AlexRiedler commented 10 years ago

yeah... I am still thinking of that case @attenzione; I might 'revert' this change; and remove this idea of 'TemplatePath' and use logical paths instead just for the template names (which means I believe the change I told you above will still work).

ggarnier commented 10 years ago

@AlexRiedler in my case, I use the default path_prefix = 'templates'. I solved my problem changing my templates path inside the gem from my-gem/component/templates/template_name to templates/my-gem/component/template_name. Now the template path is my-gem/component/template_name.