Is there a helper for creating handlebars blocks in haml? - ruby-on-rails

Because handlebars tags are evaluated after haml is compiled to html, and handlebars counts as plane text in haml you cannot indent logic
{{#if misc}}
%b Misc Products
Total:
{{misc_total}}
{{#each misc}}
{{price_charged}}
{{notes}}
{{/each}}
{{/if}}
Which is hard to read.
I made a rails helper to make this better looking*.
def handlebars_helper(helper, &block)
raise ArgumentError, "Missing block" unless block_given?
open = ActiveSupport::SafeBuffer.new("{{##{helper}}}") # helper opening
open.safe_concat capture(&block)
open.safe_concat("{{/#{helper.split.first}}}") # helper closing
concat(open)
end
which lets you write haml that looks like this
- handlebars_helper 'if misc' do
%b Misc Products
Total:
{{misc_total}}
- handlebars_helper 'each misc' do
{{price_charged}}
{{notes}}
Has this been done before, or is there a better way for me to do it?
*this helper looks a bit messy, I'm not that comfortable with blocks how would I clean this up?

If you want to use HAML to generate handlebars code, you can use hamlbars, which will provide you a few helpers to help facilitate indentation. Unfortunately you'll still be stuck with awkward indentation surrounding if/else.
I'd recommend Emblem.js as an alternative... the syntax is closer to Slim than HAML, but there's gonna be no ugly helper code polluting your templates just to preserve indentation. Disclaimer: I wrote emblem.

Related

html_safe on whole html code block in rails

i can't seem to find a way to escape whole html block, if it is even possible.
Here is what i am trying to do:
%table
%tr
%th{rowspan: "2"}= t("public.home.graphic_title_payment").html_safe
%th.datefield{rowspan: "2"}= t("public.home.graphic_title_dates").html_safe
%th{colspan: "3"}= t("public.home.graphic_title_pay_group").html_safe
%th{rowspan: "2"}= t("public.home.graphic_title_unpaid").html_safe
Altho i dont like purring .html_safe at the end of every string. Is there a way i could maybe put some kind of indicator that all %tr needs to be html_safe??
Thanks :)
Rails escapes ERB output by default. So the best thing would probably be to create a new helper method to run the I18n translation and mark it as safe. So perhaps:
# app/helpers/application_helper.rb
def st(translation)
t(translation).html_safe
end
Then:
%th{rowspan: "2"}= st("public.home.graphic_title_payment")

Using .css.scss variables in jQuery?

I am working on a Rails 3.1 app and am happily using SASS and CoffeeScript. I particularly like the SASS extensions of variables and imports. I have constructed a single _global-settings.css.scss file which contains ALL of the hex constant values I use throughout all of my stylesheets. This gives me a single place to manage colors, fonts and layout dimensions. Very DRY.
But, if I wish to use JQuery to tweak my css dynamically, I no longer have access to my css compile-time variables, and must reproduce the same data as a JSON hash in a .js.coffee file. Not DRY at all.
Here is my question: Before I go off and build a rake task to munge my css settings file into an equivalent CoffeeScript hash, does anyone have a more clever idea? Like hiding all the values in a hidden div in an html file?
You'd have an easier time moving your CSS configuration into Ruby and then sending your _global-settings.css.scss and a small piece of CoffeeScript through ERB. Then you have your settings in place and you can access them everywhere.
Somewhere in Ruby you'd have this:
CSS_SETTINGS = {
:text_color => '#333',
:text_color_hilite => '#f33',
:font_size => '14px',
#...
}
Then rename your _global-settings.css.scss to _global-settings.css.scss.erb and use things like this inside it:
$text-color: '<%= CSS_SETTINGS[:text_color] %>';
// ...
And inside a global_settings.js.coffee.erb you could have this:
window.app.global_settings = <%= CSS_SETTINGS.to_json.html_safe %>
You could even write a simple view helper that would SASSify a Hash:
def sassify(h)
h.map do |k, v|
# You might want more escaping for k and v in here, this is just
# a simple proof of concept demo.
[ '$', k.to_s.gsub('_', '-'), ': ', "'#{v}'", ';' ].join
end.join("\n")
end
and then you could say this in your _global-settings.css.scss.erb:
// Import global CSS settings.
<%= sassify(CSS_SETTINGS).html_safe %>
You could also monkey patch a to_sass into Hash and use CSS_SETTINGS.to_sass but that's probably taking things one step too far.

In Rails, how to write a helper function "who_am_i" so that a view or partial can use it to report which file it is?

When designing for layout and styling with CSS, it might be useful to add a line in the view or partial:
<%= who_am_i %>
or if using haml:
= who_am_i
so that it will print on the webpage
this is view/products/_footer.html.haml
How can this helper be written? (or is there one already in Rails?)
(my first try was to use __FILE__ and do some manipulation with string, and it works well if everything is done inside the view or partial file, but when it moves to helper, then __FILE__ becomes helpers/application_helper.rb so it won't work. But I'd like to find out possibly better ways to do it)
Your helper method should be:
def who_am_i
#template.template
end
Rails 3
def who_am_i
#_virtual_path
end
Note:
I tested the solution in Rails 3.0.5 and it works. I am not certain that it will work in all scenarios. Use with caution.

Possible to embed markdown within erb?

If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter.
Is is possible to do the same using erb?
It's pretty easy to write a method that does it, assuming you're using something like Rails which has #capture, #concat, and #markdown helpers. Here's an example, using Maruku:
def markdown_filter(&block)
concat(markdown(capture(&block)))
end
Then you can use this as so:
<% markdown_filter do %>
# Title
This is a *paragraph*.
This is **another paragraph**.
<% end %>
There are a few things to note here. First, it's important that all the text in the block have no indentation; you could get around this by figuring out the common indentation of the lines and removing it, but I didn't do that in the example helper above. Second, it uses Rails' #markdown helper, which could easily be implemented in other frameworks as so (replacing Maruku with your Markdown processor of choice):
def markdown(text)
Maruku.new(text).to_html
end
Rails 3 has removed the #markdown helper, so just add the above code in the appropriate helper, substituting the Markdown processor of your choice.
ERB does not have filtering like this built-in. You'll need to directly use a gem that handles it, like RDiscount or the venerable BlueCloth.

Issue with Haml files

Hi i converting rails views from erb to Haml .The issue i faced is when use the form_for the haml throws the UNEXPECTED $end error. I am sure i did the space indentation well with in form_for .......Even if i use "each do" loop is says the same error. if i revert the haml to erb it works fine.
Rails version i used : 2.3.2 & i installed haml gem 2.2.19 as well as haml plugin also.
my controller code :
def new
#user = User.new
end
My View code :
.contentContainer.signup
- form_for(#user) do |f|
Make sure your haml indentation is perfect.
.contentContainer.signup
- form_for(#user) do |f|
= f.text_field :name
Are you including - end in your templates? Haml takes care of ends for you, so if you add your own, it won't work.
Can you paste your entire template (in a code block, so it's formatted properly)?
There's also a good command-line tool to make transition easier: html2haml. It doesn't always produce the prettiest haml, but it certainly works.
Hey, there's even a web-based form for this: http://html2haml.heroku.com/
Generally, be sure your indentation is perfect. haml is very particular about indentation. If you use a decent editor (such as textmate or vim) this is an easy task.
If the last line in file is indented there has to be an addidtional, empty line.

Resources