The comma is split into one line in HAML

Using HAML, I would like to have links on the same line with commas between them. In this way,

Check me out on GitHub, Twitter, Coderwall and LinkedIn.

Here is what I have:

 %footer Check me out on %a{:href => url('http://github.com/bostonaholic')} Github , %a{:href => url('http://www.twitter.com/bostonaholic')} Twitter , %a{:href => url('http://coderwall.com/bostonaholic')} Coderwall and %a{:href => url('http://www.linkedin.com/in/mboston')} LinkedIn \. 

But this happens as follows:

 Check me out on GitHub , Twitter , Coderwall and LinkedIn . 

How to fix this so that the commas and period are in the right places?

Thanks.

+5
source share
3 answers

HAML reviewed this case with the succeed helper. Docs here: http://haml.info/docs/yardoc/Haml/Helpers.html#succeed-instance_method

This is Haml

 click = succeed '.' do %a{:href=>"thing"} here 

produces

 click <a href='thing'>here</a>. 
+5
source

This is a huge and long-standing problem in HAML. I personally solved this in two different ways.

1) I put the desired text in the elements of the array and use

 join(', ') 

But it's a pretty crappy way of doing things.

2) The only "solution" to HAML is a special> character. The> character removes spaces in the output html so that there are no spaces BEFORE or AFTER this tag is displayed.

This is the best I could think of, and I lose sleep at night because of this terrible disgrace.

  %a{:href => 'here'} Github %span> ,&nbsp; %a{:href => 'there'} Twitter 

Notes on this latest technique.

a. If you are not using & nbsp; you will not get a space, even if it is included in the source code. Using quotes (% span> # {","}) and a space do not work, as it is obvious that HAML truncates the output.

b. You should use the tag because using> only works after the tag, as far as I can tell. Using =>, unfortunately, will not work. This would be my recommended way to do this, but it would not solve the last lost area anyway.

3) In my rails projects, I use the following combination of partials and helpers:

partial: _haml_comma.html.haml

 %span> ,&nbsp; 

helper.rb

  def comma render :partial => "shared/haml_comma" end 

.haml file:

  %a{:href => 'here'} Github = comma %a{:href => 'there'} Twitter 

4) If you have ENUMERABLE and LOOPING, the following auxiliary and partial

application_helper.rb

  # puts a comma after an element in a loop EXCEPT the last loop def comma_separate(i:, total:, &block) render layout: 'shared/comma_separate', locals: {i:i, total: total} do capture(&block) if block end end 

views / shared / _comma_separate.html.haml:

 = succeed i + 1 < total ? ', ' : nil do %span.some_class< = yield 

in your opinion:

 - enumerable.each_with_index do |w, i| = comma_separate i: i, total: enumerable.size do %b = w 

(This change was made in 2019, 5 years after the first comment. Why is this not resolved by the accompanying HAML?)

+5
source

Using the haml 'success' helper (thanks @josal), here is a simple and easy way to do this:

 %footer Check me out on = succeed ',' do %a{:href => url('http://github.com/bostonaholic')} Github = succeed ',' do %a{:href => url('http://www.twitter.com/bostonaholic')} Twitter = succeed 'and' do %a{:href => url('http://coderwall.com/bostonaholic')} Coderwall = succeed '.' do %a{:href => url('http://www.linkedin.com/in/mboston')} LinkedIn 

I am about the only time it is necessary when you work with links, but it is a good tool for your haml belt.

+1
source

Source: https://habr.com/ru/post/1379586/


All Articles