GeekedOut
GeekedOut

Reputation: 17185

HAML - how to put regular text between links

I have a small HAML paragraph with links like this:

%p
  =link_to("My Disclosures", "#")
  =link_to("Create Disclosure", "#")
  =link_to("My Programs", "#") 
  =link_to("Log Out", "#")

What I want to do is to put a separator like this "|" between the links, but I am not sure how. I tried putting it next to the text inside the "" but it became part of the link text. When I tried putting the separators at the end of the line, it just didn't show up.

Any idea how I can get the serperating character to render as just text after a link?

Thanks!

Upvotes: 2

Views: 1406

Answers (3)

superluminary
superluminary

Reputation: 49232

In this instance you're going about things the wrong way.

A much more correct way to get the effect you are after would involve a little CSS:

ul.nav {
  list-style:none;
  margin:0;
  padding:0
}

ul.nav li {
  margin:0;
  padding:0 0.5em;
  border-right:1px solid black
}

ul.nav li.last {
  border-right:none;
}

%ul.nav
  %li=link_to("My Disclosures", "#")
  %li=link_to("Create Disclosure", "#")
  %li=link_to("My Programs", "#") 
  %li.last=link_to("Log Out", "#")

This way you have a plain, semantically correct, accessible and search engine friendly list of links which converts to a styled list using the magic of CSS.

I am aware I am answering a different question to the one you asked, but you should be aware of the "correct" way to tackle this use case.

Upvotes: 1

jdoe
jdoe

Reputation: 15779

I had the very same problem. This how I solved it:

1) in module ApplicationHelper:

def links_bar(join_with = ' | ', &block)
  capture{yield}.split("\n").join(join_with).html_safe
end

2) in any view:

= links_bar do
  = link_to("My Disclosures", "#")
  = link_to("Create Disclosure", "#")
  = link_to("My Programs", "#") 
  = link_to("Log Out", "#")

Generates:

<a href="#">My Disclosures</a> | <a href="#">Create Disclosure</a> | <a href="#">My Programs</a> | <a href="#">Log Out</a>

Am I crazy? ;)

Thanks to Semyon Perepelitsa for hints.

Upvotes: 4

Matt Huggins
Matt Huggins

Reputation: 83319

You can just write the string itself on its own line:

%p
  =link_to("My Disclosures", "#")
  |
  =link_to("Create Disclosure", "#")
  |
  =link_to("My Programs", "#") 
  |
  =link_to("Log Out", "#")

Alternatively, you can append it to your output string:

%p
  =link_to("My Disclosures", "#") << '|'
  =link_to("Create Disclosure", "#") << '|'
  =link_to("My Programs", "#") << '|'
  =link_to("Log Out", "#")

Upvotes: 5

Related Questions