In my Rails template, I\'d like to accomplish final HTML to this effect using HAML:
I will first link somewhere
Although not well documented, this is cleanly achieved using HAML whitespace preservation (>) combined with an ASCII space (& #32;), and not with helpers:
%a{:href=>'/home'}> Home link
, 
%a{:href=>'/page'} Next link
This will produce what you want:
<a href='/home'>Anchor text</a>, 
<a href='/page'>More text</a>
But I agree, HAML needs to come up with a better way of doing this, as it does add unnecessary ASCII characters to the page (but it's still more efficient than using helpers).
Yet another option that I've used in the past:
- if @condition
%span> , then some more text after the link.
The preserve function worked for me
.white-space-pre= preserve "TEXT"
You can use the 'aligator syntax' of HAML
Whitespace Removal: > and <
and < give you more control over the whitespace near a tag. > will remove all whitespace surrounding a tag, while < will remove all whitespace immediately within a tag. You can think of them as alligators eating the whitespace: > faces out of the tag and eats the whitespace on the outside, and < faces into the tag and eats the whitespace on the inside. They’re placed at the end of a tag definition, after class, id, and attribute declarations but before / or =.
http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_
The solution that I got working is:
I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
= ", then render this half of the sentence if a condition is met"
You can use =
, though =
is used to output the result of Rails code, but here it will server the purpose.
A better way to do this has been introduced via Haml's helpers:
= surround '(', ')' do
%a{:href => "food"} chicken
Produces:
(<a href='food'>chicken</a>)
click
= succeed '.' do
%a{:href=>"thing"} here
Produces:
click
<a href='thing'>here</a>.
= precede '*' do
%span.small Not really
Produces:
*<span class='small'>Not really</span>
I will first
= succeed ',' do
= link_to 'link somewhere', 'http://example.com'
- if @condition
then render this half of the sentence if a condition is met
Produces:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met