Passing raw HTML to haml file - ruby-on-rails

Is it possible to pass pre formatted HTML to the haml file. For example I pass a variable such as:
my_text = "<b>this is bold</b>"
Then in my haml file:
%p
=#my_text
I was hoping it would display This is bold
But it just returns the original string and ignores the tags surrounding "this is bold"
The goal is to highlight certain key words("one" and "two" in this example), here's a better example:
#my_text = "This <b>one</b> plus <b>one</b> is a total of <b>two</b>"

Not sure what you want to achieve, but i'd recommend that you keep your markup in the haml and plug in your copy in the instance variable like this.
MyTextController.rb
#my_text = this is bold
my_text.html.haml
%b
= #my_text
Edit after further clarification.
You can use sanitize helper for this.
my_text.html.haml
%p
= sanitize(#my_text, tags: %w(b))

Ahh I figured it out, looks like you can do:
%p
= raw #my_text

Related

Have trouble implement the variable loop in haml file

I use ruby on rails to get some variables and suppose to send them in email format to some email address.
%div
- #msg.each do |line|
%p = "#{line}"
%br
The msg is the string array I passed in and would like to get each element in separate line. How can I achieve that. The above code won't work.
One of the key things of coding HAML is you really are only going to have one item per line, plus you automatically get a div tag as the default tag. So you might as well add a class to your div to differentiate it. Additionally, make sure your indentation is correct:
.messages
- #msg.each do |line|
%p= line
You don't really need a br because the p will break for you, and following a tag with '=' will automatically give you the interpolation.

How to show variables from Ruby in HTML

I have many variables like:
#a = '<b>bla bla</b>'
They appear in the browser in the same way. I need code that can translate them into HTML so that they will appear as:
bla bla
Use this to print it as HTML
#a = '<b>bla bla</b>'.html_safe
html_safe() will not escape any characters.
Maybe you need to use html helpers?
http://sraji.wordpress.com/2011/05/26/how-to-display-html-tags-in-rails-helper-methods/
http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/

Getting strings from i18n YAML file?

I have some view code:
<span data-something="[<%= t('.asd') %>, <%= t('.dsf') %>]></span>
I use this code to get some dynamic strings translated into the view. My YAML is something like:
en:
feature:
asd: "Asdddd"
dsf: "adasdsadasda"
Is there a way I can use to dynamically get all the "features" from the YAML by locale and put it in the data-attribute?
This is pretty simple to do:
I18n.translate('feature').values.join(', ')
You'll end up with a string "Asdddd, adasdsadasda".
I think it's as easy as t('feature') to get the hash, you might want to just have the values so could you try t('feature').keys?

How do I use HAML in a dynamic link?

I am trying to create a link using HAML which looks like the this
=link_to("Last updated on<%=#last_data.date_from.month %>",'/member/abc/def?month={Time.now.month}&range=xyz&year={Time.now.year}')
It is not taking the Ruby code and it is displaying that as a string
Last updated on<%=#last_data.date_from.month %>
and in the URL as well it is not taking the function Time.now.month or Time.now.year .
How do I pass Ruby code in URL and in the string ?
You should probably use something like this:
= link_to("Last updated on #{#last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}")
Note that in the second string, it's necessary to change the ' to ". Also if the link text is getting long, you can use something like this:
= link_to("/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}") do
Last updated on #{#last_data.date_from.month}
Everything after the = in HAML is a Ruby expression. Ruby doesn't interpolate strings the way HAML does, it has own way of such interpolation.
In Ruby, when you want to have string value of some variable inside another string, you could do.
"Some string #{Time.now}"
So, it should be:
= link_to "Last updated on #{#last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}"
A simple example with easy syntax:
link_to "Profile #{rubycode}", "profile_path(#profile)/#{ruby_code}", class: "active"

How can I get Haml to render the contents of a pre tag correctly?

I read the Haml docs where they talk about the pre tag and "preserving whitespace". According to the docs, pre "preserves whitespace" by default and you need to use the ~ operator to output the contents of the tag to get it to render correctly. Following the recommended practice, I have this:
%pre
~ #calendar.main_template
The output in the browser:
(This may be a little confusing -- the app is letting the user manipulate Haml code, so I'm actually displaying Haml code here in the UI.)
%div
= events
What output want:
%div
= events
I also tried using = instead of ~. Also tried %pre>, %pre<, and %pre>< all with identical results.
You want preserve.
%pre
= preserve "I like\n Cheese"

Resources