Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HAML Coffee Assets with Sinatra? #37

Closed
michaltaberski opened this issue Apr 10, 2012 · 14 comments
Closed

Use HAML Coffee Assets with Sinatra? #37

michaltaberski opened this issue Apr 10, 2012 · 14 comments

Comments

@michaltaberski
Copy link

Hi,

can I use this gem with Sinatra, and if yes could anybody indicate me the easiest solution to do this?

Thx in advance

Michał

@netzpirat
Copy link
Collaborator

haml_coffee_assets registers itself as tilt engine, so you can simply require the gem and start using hamlc views with sinatra.

@michaltaberski
Copy link
Author

thx a lot

@paulyoung
Copy link
Contributor

Could you provide an example? Thanks.

@paulyoung
Copy link
Contributor

I don't see this working as expected. This returns nothing:

# Lookup a template class for the given filename or file
# extension. Return nil when no implementation is found.
puts Tilt.[]('.hamlc')

@netzpirat netzpirat reopened this Aug 1, 2012
@netzpirat
Copy link
Collaborator

Just returned from holidays. I'll have a look this week.

@netzpirat
Copy link
Collaborator

So, I finally took some time and played with the "Tilt" integration, since it was provided by a pull request and not written by myself. First I noticed that the Haml Coffee Assets headline '.. or as Tilt template' was misleading, because it's a Sprocket engine that confirms to the Tilt template interface. This means that you can use Sprockets to compile and serve the Haml Coffee Assets to the client side, but you can't use it on the server side as Tilt template. This makes totally sense, since you'd better use Ruby Haml on the server side.

I released the demo application Haml Coffee Sinatra, so you can see how to set up Sprockets in Sinatra to use Haml Coffee.

@paulyoung
Copy link
Contributor

Thanks for the demo app. I was already doing something similar by reading the erb file in the vendor directory, which works without Sprockets but feels like a hack.

Could there be a public method which exposes the contents of this file in a reliable way instead?

@netzpirat
Copy link
Collaborator

Which file do you mean? The Haml helpers?

@paulyoung
Copy link
Contributor

vendor/assets/javascripts/hamlcoffee.js.coffee.erb

I'm accessing it like this:

File.join Bundler.load.specs.find{ |s| s.name == 'haml_coffee_assets' }.full_gem_path, 'vendor', 'assets', 'javascripts', 'hamlcoffee.js.coffee.erb'

but I'm concerned that the file may move at some point and my implementation will break.

@netzpirat
Copy link
Collaborator

I just released 1.4.0 that allows you to get the helper functions as JavaScript String:

HamlCoffeeAssets.helpers

or you can also get the CoffeeScript version if you need to:

HamlCoffeeAssets.helpers(false)

@paulyoung
Copy link
Contributor

Awesome.

Now this:

# Access the HamlCoffee erb template from the gem
spec  = Bundler.load.specs.find{ |s| s.name == 'haml_coffee_assets' }
path  = File.join spec.full_gem_path, 'vendor', 'assets', 'javascripts', 'hamlcoffee.js.coffee.erb'
erb   = File.read path

# Compile the template from ERB to CoffeeScript to JavaScript
coffeescript  = ERB.new(erb).result()
hamlcoffee    = CoffeeScript.compile coffeescript

Becomes:

HamlCoffeeAssets.helpers

@netzpirat
Copy link
Collaborator

Great. And there are specs for this, so whenever I feel to change it, I'll not break your app :P

@paulyoung
Copy link
Contributor

I did realize that when I removed coffee-script from my Gemfile, haml_coffee_assets complained.

This doesn't seem right to me.

@netzpirat
Copy link
Collaborator

Oh, now I remember. Haml Coffee Assets has a skeleton in the closet: I wasn't able to get the haml-coffee compiler running with the coffee-script gem, so I included my own version of CoffeeScript and used only execjs as dependency. I'll have a look again to see if I can finally get it right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants