views:

1389

answers:

7

In my Rails template, I'd like to accomplish final HTML to this effect using HAML:

I will first <a href="http://example.com"&gt;link somewhere</a>, then render this half of the sentence if a condition is met

The template that comes close:

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 may, however, note that this produces a space between the link and the comma. Is there any practical way to avoid this whitespace? I know there's syntax to remove whitespace around tags, but can this same syntax be applied to just text? I really don't like the solution of extra markup to accomplish this...

Thanks!

A: 

There's the angle bracket "whitespace munching" syntax, otherwise write a helper method for it.

Andrew Vit
How exactly would a helper for that work? Meh, I'll see what I can come up with...
Matchu
As for whitespace munching, I can't work out how to make that syntax work if it's not on some sort of tag definition. Am I just doing it wrong, or does that syntax not work without a tag?
Matchu
+4  A: 

Alright, here's the solution I'm settling on:

Helper

def one_line(&block)
  haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end

View

I will first
- one_line do
  = link_to 'link somewhere', 'http://example.com'
  - if @condition
    , then render this half of the sentence
    \\n
    if a condition is met

That way, whitespace is excluded by default, but I can still explicitly include it with a "\n" line. (It needs the double-backslash because otherwise HAML interprets it as an actual newline.) Let me know if there's a better option out there!

Matchu
Note to world: I eventually wised up and moved these to helpers :P
Matchu
A: 

Once approach I've taken to this sort of thing is to use string interpolation:

I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}

I don't like the look of the literal string in the interpolation, but I've used it with previously declared strings or dynamically generated strings before.

Chuck
Mhm. That's my general approach to things like that, but I had never thought of using the conditional in there. It's a shame that the actual template I was using turned out to be a bit more complex than just a second half of a sentence... but that's definitely worth remembering - thanks!
Matchu
+3  A: 

You can also do this using Haml's "trim whitespace" modifier. Inserting > after a Haml declaration will prevent whitespace from being added around it:

I will first
%a{:href => 'http://example.com'}&gt; link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

produces:

I will first<a href='http://example.com'&gt;link somewhere</a>, then render this half of the sentence if a condition is met

However, as you can see, the > modifier also strips the whitespace in front of the link, removing the desired space between the words and the link. I haven't figured a pretty way around this yet, except to add &nbsp; to the end of "I will first", like so:

I will first&nbsp;
%a{:href => 'http://example.com'}&gt; link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

Which finally produces the desired output without lots of hard-to-read interpolation:

I will first&nbsp;<span><a href="http://example.com"&gt;link somewhere</a></span>, then render this half of the sentence if a condition is met
Ryan Heneise
Forgot to mention that I got this from the Haml cheat sheet, which is very helpful: http://cheat.errtheblog.com/s/haml/
Ryan Heneise
It works with tags but not expressions; in your example you've changed his expression to a tag. I have the same issue and unfortunately this is not a solution.
Teflon Ted
A: 

Ryan - you can do this to keep the leading space:

%a{:href => 'http://example.com'}>= ' link somewhere'

The space is in the quotes.

thethinman
Hmm - just realized that doesn't work if underline is on
thethinman
This works: %a{:href => 'http://example.com'}>= 'link somewhere'
thethinman
A: 

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
,&#32; 
%a{:href=>'/page'} Next link

This will produce what you want:

<a href='/home'>Anchor text</a>,&#32;
<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).

ojak
A: 

I came across a similar problem and found this so I thought I would post another solution which doesn't require a helper method. Use Ruby interpolation #{} to wrap the link and if statements:

I will first 
#{link_to 'link somewhere', 'http://example.com'}#{if true : ", then render this half of the sentence if a condition is met" end}

This works in 3.0.18, it may also work in earlier releases.

ikezue
Granted, Haml isn't designed for content. This isn't content that we're talking about there, though. It's a template. The author of that blog post is referring to things like writing a full static webpage in Haml, which isn't what I'm doing. The code snippet I provided is pretty much the full `.haml` file — the fact that it includes a link and a comma doesn't really indicate anything either way.
Matchu
Ah, I see. I've edited my answer, leaving the solution to stand on its own.
ikezue