25

In my Ruby on Rails Project I use HAML, I need to translate something like this

<div>foo <a>bar</a>.<div>

into HAML. It should look like:

.divclass
  foo
  %a bar
  .     

The period at start is not working because its used by HAML. So how can I use a period as content?

Even with building a span around its not working, again the period is taken as something special.

I think there is an escape mechanism but I can't find it.

user229044
  • 232,980
  • 40
  • 330
  • 338
Calmon
  • 583
  • 5
  • 18
  • 4
    In HAML, as with most other languages, things are escaped with a backslash. It should be the first thing you try when moving to a new language. – user229044 Sep 20 '12 at 12:57
  • HAML is also well documented: http://haml.info/docs/yardoc/file.HAML_REFERENCE.html – mwolfetech Sep 20 '12 at 12:59

4 Answers4

39

It is escaped with \

like this

\.

See Escaping \ in the HAML reference.

Update: Using HAML's succeed (as in this answer) is a better solution if you need a dot in the end of the sentence (to prevent unnecessary white space).

Community
  • 1
  • 1
khustochka
  • 2,346
  • 20
  • 29
  • 1
    The problem here is that in his case there will be a white space between the a tag and the dot. Prefer the `succeed` method from @Jay – Jeremy F. Jun 05 '15 at 14:17
20

A better method:

%p
  This sentence ends with a link which is *just* before a period
  = succeed "." do
    %a{:href => "#"} link
  But the period wasn't included with the link and there was no space before the period.
Jay
  • 1,086
  • 11
  • 25
2

You also have access to the :plain filter, like this:

%p 
  :plain
    .
superluminary
  • 47,086
  • 25
  • 151
  • 148
2

I'm going to add another option using Rails' built-in link_to helper instead of manually creating the link. Then you can simply do

%p
  This sentence ends with a link which is *just* before a period
  #{ link_to "link", "#" }.

Different solutions will work in different places, but I find this to be quite clean, especially if already using link_to or mail_to etc.

zelanix
  • 3,326
  • 1
  • 25
  • 35
  • This is definitely the way. Since we're clearly need interpolation here from the generic point of view. – jibiel May 15 '18 at 09:42