45

When I'm saying:

%p= item.price + " dollars"

I'm getting

50&nbsp ;dollars

instead of having non-breakable space symbol.

How to insert this and another special symbols using HAML ?

AntonAL
  • 16,692
  • 21
  • 80
  • 114

7 Answers7

75

How about

%p= item.price + " dollars".html_safe
HakonB
  • 6,977
  • 1
  • 26
  • 27
  • 1
    also useful to insert non-breaking space in link_to, for example = link_to "Some Link".html_safe, "/someurl" – jpw Aug 29 '12 at 19:18
  • I found that this strategy doesn't actually work. I believe what's happening is that the first string (item.price) is not html safe. When you do the addition, you end up with a string that is not marked html_safe and you end up with the &nbsp showing up in the view. I like the answer below (where you interpolate and then html_safe). The other option is render on two lines - item.price on one (without html safe) and the  dollars on the next line. – mr rogers Mar 04 '13 at 23:28
  • I see your point. I guess something has changed during the 20 months since I wrote this answer. I then much prefer the two line approach instead of marking the item.price as html_safe... – HakonB Mar 13 '13 at 21:14
  • sweet! this helped me too. – Priya Ranjan Singh Aug 07 '13 at 11:56
  • although all you really need to do in haml is either break out of ruby (no equal sign) and use string interpolation, %p #{item.price} dollars, or create a newline with the   dollars. This is most likely a recent haml update since this was posted in '11 – engineerDave Sep 25 '15 at 21:47
15

Use != instead of =

See "Unescaping HTML" in the haml reference: http://haml.info/docs/yardoc/file.REFERENCE.html#unescaping_html

Adam Fraser
  • 6,255
  • 10
  • 42
  • 54
14

I tried using html_safe in different ways, but none worked. Using \xa0 as suggested by ngn didn't work, either, but it got me to try the Unicode escape of the non-breaking space, which did work:

"FOO\u00a0BAR"

and .html_safe isn't even needed (unless something else in the string needs that, of course).

The Ruby Programming Language, first edition, says: "In Ruby 1.9, double-quoted strings can include arbitrary Unicode escape characters with \u escapes. In its simplest form, \u is followed by exactly four hexadecimal digits ..."

Teemu Leisti
  • 3,750
  • 2
  • 30
  • 39
14

The interpolation option:

%p= "#{item.price} dollars".html_safe
D. Simpson
  • 1,882
  • 17
  • 32
  • 4
    While I didn't downvote your answer I think it does have potential problem as it marks item.price as html safe which may cause a XSS issues... Given the context in this question we cannot tell for sure :-) – HakonB Jun 13 '11 at 15:13
  • Quite so, but the answer that was upvoted twice uses the same html_safe option, moreover, that's about the only way you're going to get HAML to output an inline special character like that. – D. Simpson Jun 14 '11 at 03:53
  • But you're answer is marking contents of `item.price` as html_safe, which is where the potential problem arises. The other answer marks a string constant as html_safe which is a better solution. – Peter Coulton Sep 07 '11 at 19:16
  • For certain you may be right. Like HakonB mentioned earlier, given the context of the question, we can't tell for sure what, if any, issues might arise by interpolating the `item_price` - it's just another possible answer to the question. – D. Simpson Sep 08 '11 at 01:47
4

This answer is for a slightly different question but I found this question searching for it...
If you have a submit tag %input{ :type => "submit", :value => " dollars", :name => "very_contrived" } even if you throw an html_safe on the :value it will not evaluate the html.
The solution is to use the rails helper... duh

= submit_tag " dollars".html_safe

this is pretty obvious but it tripped me up. Legacy code + rails upgrade = this kind of stuff :P

mraaroncruz
  • 3,780
  • 2
  • 32
  • 31
0

I prefer using the character itself with the escaped HTML method with most symbols other than the whitespace characters. That way i don't have to remember all the html codes. As for the whitespace characters i prefer to use CSS, this is a much cleaner way.

%p&= "#{item.price} $%&#*@"
0

You could use \xa0 in the string instead of  . 0xa0 is the ASCII code of the non-breaking space.

ngn
  • 7,763
  • 6
  • 26
  • 35