Open deepak opened 11 years ago
have not written any tests sorry. also cannot figure out the whole flow.
TODO
tried
initializer 'handlebars.prepend_views_path', :after => :add_view_paths do |app|
ActionController::Base.class_eval do
before_filter do |controller|
prepend_view_path 'app/assets/javascripts/templates'
end
end
end
and
initializer 'handlebars.append_assets_path', :after => :append_assets_path, :group => :all do |app|
Dir[Rails.root.join("app", "views", "*")].each do |path|
app.assets.paths << path
end
end
How to you peeps share templates on the client and the server ? also is there any pure-ruby handlebars implementation ?
@deepak glad my code was of some help to you. A few notes though:
thanks @zohararad
I liked the convention for to_hbs
:+1:
but did not use it as i was already using active_model_serializer
I am using active_model_serializer
like in https://github.com/rails-api/active_model_serializers/issues/192
and rendering with <%= render 'loan', Loan.active_model_serializer._root => loan.as_json(root: false) %>
not the cleanest of solutions :-)
I tried using prepend_view_path
for template path definations. But was getting errors
I think it was because i had a index.html.erb and index (hbs file) both
Hi @deepak,
Looks good, but needs at least a minimal rendering test :)
A note from someone who was a duplicate set of erb/hbs templates:
:shipit:
This is a killer feature.
This would be really nice to have.
You could actually make the ActionView Handler less concrete; but that is a matter of opinion.
My changes ( https://github.com/leshill/handlebars_assets/pull/67 ) should help with this as well as asset-url and other rails helpers are able to be passed in during precompile which helps a fair bit.
You should also do a conditional check to save resources + not need another required gem; otherwise :+1: I was going to write this today... If this is dead I wouldn't mind taking it over as well :smile:
^ another note I should probably add is we should be running against the precompiled templates instead of recompiling in the ActionView handling.
Hi @AlexRiedler,
That would be great!
This is done @ http://github.com/AlexRiedler/handlebars_assets I just need to make sure everything is working ... it is quite a big change and may only make it into the release of JSAssets in the end (I need to clean it up).
I'd love to see this feature. What's the current status? Is this PR from @deepak good, just waiting for tests? Or is the fork from @AlexRiedler furthest along?
What are the missing pieces that someone could help with to see this feature in the main project?
@turadg I have been working on it the past week or so; there is still some kinks in it that I am trying to workout.
Short List (of hacks I don't like):
I believe the correct way to do this is probably give a folder e.g. "javascripts/templates"; or say everything that registered with HandlebarAssets in Sprockets should be loaded into the context. Downsides to this are on boot-up the application must precompile all the templates (which means not so great compilation errors, especially coming out of the ExecJS context).
I also am quite busy in my personal life (shit happens); I will see what I can do this week to my branch to make it a little more sane.
:+1: I hope this makes it in soon!
Little update! I tried to use @AlexRiedler's fork as-is in my project (by directing the gem statement to that repo). It didn't work: I was getting undefined method 'fresh?' for nil:NilClass
, which is a lovely error and gave me a good laugh.
Failing that, I tried adding @deepak's changes into my local copy of handlebars_assets
, and this worked. Now I can have the following file,
app/assets/javascripts/templates/_widget.jst.hbs
and do this in a view,
render '../assets/javascripts/templates/widget', name: "bob"
The handlebars template will be rendered. However, I haven't been able to figure out how to pass an object rather than key value pairs like name: "bob"
above. Also, and more importantly, the widget file has to be _widget
to be recognized by rails as a partial... but sprockets won't compile files with a leading _
into functions.
This is a really cool feature, I'd love to be able to have just one .jst.hbs
file called widget
that would server as both the first-class view template on my client side (compiled into he JST), and as a partial to be pre-rendered on the server-side. Alas, today is not the day.
EDIT: today is the day!
render template: '../assets/javascripts/templates/widget', locals: widget.to_hbs
where to_hbs
is basically JSON.parse(widget.to_json)
. This lets me have all my cake!
I might be able to get around to this relatively soon (by end of the month); I have been battling some personal issues over the last couple months and hence why the lack of movement in the project. I still would like to get this actively working though as well. I had it working with my personal branch but it did require some additional modifications to the rails setup environment (to which I can no longer remember quite perfectly); and I also ran into some issues with development environment not refreshing properly in various cases which was bothersome.
My fork from master and has @deepak's changes, if that helps. I'm currently able to do the stuff.
Ready to close? :)
(it's showing up in my list of open issues in which I've been mentioned)
using ExecJS and the handlebars javascript source the handlebars templates is rendered on the server
objective is to keep templates DRY. same template on the client and server
stole code from https://github.com/zohararad/handlebarer/blob/master/lib/handlebarer/renderer.rb#L24-L34 to introspect locals and controller variables no need to pass in a special
handlebars
local like https://github.com/railsware/sht_railscopied the api for https://github.com/pixeltrix/steering as well
thanks @zohararad and @pixeltrix :-)