handlebars_assets icon indicating copy to clipboard operation
handlebars_assets copied to clipboard

Problem with template path since v0.17

Open ggarnier opened this issue 11 years ago • 13 comments

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/template_name.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 full_path parameter. This explains the behavior change, as I use the default path_prefix value (which is templates).

ggarnier avatar Jun 26 '14 17:06 ggarnier

@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).

AlexRiedler avatar Jun 26 '14 17:06 AlexRiedler

@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.

ggarnier avatar Jun 26 '14 17:06 ggarnier

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

attenzione avatar Jul 03 '14 09:07 attenzione

@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 avatar Jul 03 '14 12:07 AlexRiedler

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

AlexRiedler avatar Jul 03 '14 12:07 AlexRiedler

@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

attenzione avatar Jul 03 '14 12:07 attenzione

@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 avatar Jul 03 '14 12:07 AlexRiedler

@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)

AlexRiedler avatar Jul 03 '14 13:07 AlexRiedler

@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

attenzione avatar Jul 03 '14 13:07 attenzione

@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).

AlexRiedler avatar Jul 03 '14 13:07 AlexRiedler

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

attenzione avatar Jul 03 '14 13:07 attenzione

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).

AlexRiedler avatar Jul 03 '14 13:07 AlexRiedler

@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.

ggarnier avatar Jul 03 '14 13:07 ggarnier