How can I use haml within a coffeescript file? - ruby-on-rails

Is it possible to use haml within a coffeescript file in a rails 3.1 project?
What is the correct order of the file extensions?
My last try was that:
home.js.haml.coffescript
$ ->
alert '#{#count}'
where #count is a ruby variable.

The correct order would be home.js.coffeescript.haml—you want the file to first be evaluated as Haml to give you your variables, then compiled as CoffeeScript, then finally served as JavaScript.
However, I strongly suspect that the Haml processor will choke on some CoffeeScript syntax. It would probably be safer to use ERB, which should work for your example.

Related

Why naming convention of views files in rails are considered as action.html.erb only?

Why naming convention of views files in rails are considered as action.html.erb only ( instead of action.erb.html ) ?
What will happen if we write views files as action.erb.html ?
What will happen if we write views files as action.erb.html?
The first thing that will happen is that the Ruby syntax highlighting in your editor will stop working as the file extension is now .html instead of .erb. On almost every file system in use the file extension is the rightmost part of the file name.
The second thing that will happen is that Rails will no longer be able to lookup the template and even if it could it would no longer process it through ERB as it no longer has the .erb file extension.
.html is just a segment of the file name that lets the rails template resolver distinguish between templates for different formats when looking up a template for a given request format. Its not really technically part of the extension. For example:
show.html # just HTML - no processing
show.html.erb # a HTML ERB template
show.html.slim # a HTML Slim template
show.html.haml # a HTML Haml template
show.xml.erb # a XML ERB template
show.xml.slim # a XML Slim template
show.xml.haml # a XML Haml template
show.json.erb # a JSON ERB template
show.json.jbuilder # a JSON jBuilder template
TRDL; changing the file extension is a dumb idea. Especially when you consider that Rails actually supports multiple template engines such as jbuilder, Slim and Haml in addition to ERB.

Use HTML instead of HAML with Ruby on Rails Happy Seed

How can I use html.erb instead of haml when using a rails application that is generated using happy seed?
You dont have to do anything special.
You could convert your haml code to erb using https://haml2erb.org and then create an erb file pasting the code. Also delete the haml file. This should work.

How to make RubyMine parse .scss.erb correctly?

I am using Ruby on Rails 4 on RubyMine 6. I have a somefile.css.scss.erb file in app/assets/stylesheets/somefolder. I am using SCSS and I need ERB to use asset_url helper to write paths for background properties.
The problem is that when I use both SCSS and ERB extensions I get "cannot find variable" warning everywhere I use SCSS variables(In uses but not in initializations). Is there any way to make RubyMine parse .scss.erb files correctly?
It works fine. I'm just not comfortable with these warnings.
You don't need to do that. Just rename the file to somefile.css.scss and use the SCSS image-url helper.
image-url("rails.png")
is translated into
url(/assets/rails.png)

Evaluate SCSS (SASS) templates in ruby (Rails project)

I am working on a sort of CMS project where I am leveraging SCSS. I would like to allow the user to specify properties of a stylesheet in a simple way (enable a few color customization), and then generate a CSS file based on SCSS templates, and substitute some variables in the SCSS file using mustache or ERB evaluation.
Basically, I want an ERB file to be rendered as scss file, and then generate a css in my application, upload it to S3, and include in the user's layout.
If possible I would like to avoid using css.erb files :-)
I fact (I am answering my own question). What I am trying to do is really easy. I've made this really simple script:
#!/usr/bin/env ruby
#processs.rb file
require 'sass'
result = Sass.compile open(ARGV[0]).read
puts result
And it generates css out of a scss file she invoked like this:
ruby process.rb myfile.css.scss
And this works perfectly. The code documentation in sass source code helped me to find this out.

Rails with backbone-rails: asset helpers (image_path) in EJS files

I have a Rails 3.1 app that uses the codebrew/backbone-rails. In a .jst.ejs template, I would like to include an image, like so:
<img src="<%= image_path("foo.png") %>"/>
But of course the asset helpers are not available in JavaScript.
Chaining ERB (.jst.ejs.erb) does not work, because the EJS syntax conflicts with ERB.
Here is what I know:
The asset helpers are not available in the browser, so I need to run them on the server side.
I can work around the problem by making the server dump various asset paths into the HTML (through data attributes or <script> and JSON) and reading them back in JS, but this seems rather kludgy.
Is there a way to somehow use the asset helpers in EJS files?
There is a way, actually, to chain a .jst.ejs.erb file, although it's fairly undocumented, and I only found it through looking at the EJS test cases. You can tell EJS to use {{ }} (or [% %] or whatever else you want) instead of <% %>, and then ERB won't try to evaluate your EJS calls.
Make sure to require EJS somewhere in your code (I just included gem 'ejs' in my Gemfile), and then create an initializer (I called it ejs.rb) that includes the following:
EJS.evaluation_pattern = /\{\{([\s\S]+?)\}\}/
EJS.interpolation_pattern = /\{\{=([\s\S]+?)\}\}/
Then just make sure to rename your templates to .jst.ejs.erb, and replace your existing <% %> EJS-interpreted code with {{ }}. If you want to use something other than {{ }}, change the regular expressions in the initializer.
I wish there were an option in Sprockets to handle this through the config rather than having to explicitly include EJS, but as of the moment, there's no way to do that that I know of.
I can see two ways. Neither are great.
When you say <%%= variable %> then this is rendered by ERB as <%= variable %>, so you could double percent escape everything but the asset_tags and that would survive the trip through one ERB pass on the way to EJS.
If you find that too gross...
How about making a different javascript file, with an ERB extension, that defines your asset paths? And then use the asset pipeline to require that.
So say assets.js.erb defines something like:
MyAssets = {
'foo': <%= image_path("foo.png") %>,
...
}
And then require this somewhere near the top of your manifest. And then reference the globals however that works in EJS.
For those willing to try HAML instead of EJS: Using haml-coffee through haml_coffee_assets has worked well for me as well.
You can have the following in a .hamlc.erb file:
%img(src="<%= image_path('foo.png') %>")
(It still doesn't give you routing helpers though, only asset helpers.)
Ryan Fitzgerald was kind enough to post a gist of his JavaScript asset helpers (which get precompiled with ERB): https://gist.github.com/1406349
You can use corresponding Javascript helper via the following gem:
https://github.com/kavkaz/js_assets
Finally (after installing and configuring) you will be able to use it like this:
<img src="<%= asset_path("foo.png") %>"/>

Resources