How to allow end of line | (pipe) in HAML?

The problem is that | at the end of a line (separated by a space) is recognized as the syntax for extended line breaks. What if you want to get this character as output?

Example

Suppose you want to create a menu like

 Section 1 | Section 2 | ... 

Note: if this is exactly what you need, see concatenate link_to with pipe .

Shows the presence or absence of links, depending on a specific condition. In HAML / Ruby on Rails, it might look like it doesn't work

 %div.menu -if condition1? #{link_to 'Section 1', section_1_path} | -if condition2? #{link_to 'Section 2', section_2_path} | -if condition3? ... 

Work around

As a (somehow dirty) workaround, I changed the code:

 %div.menu -if condition1? #{link_to 'Section 1', section_1_path} #{'|'} -if condition2? #{link_to 'Section 2', section_2_path} #{'|'} -if condition3? ... 
+4
source share
4 answers

There is no need for escape access, the same indentation as the element you want to split.

 %div.menu -if condition1? #{link_to 'Section 1', section_1_path} | -if condition2? #{link_to 'Section 2', section_2_path} | -if condition3? ... 

you can try this in the browser: haml online editor: rendera or html2haml

+9
source

Haml analyzer looking for symbol | preceded by a space to indicate a multi-line block. You can use this to create a workaround using an HTML symbol link instead of the usual space in your Haml:

 %div.menu -if condition1? #{link_to 'Section 1', section_1_path} | -if condition2? #{link_to 'Section 2', section_2_path} | -if condition3? ... 

Thus, Haml will not see the space, so it will treat the channel as a literal, but the space will appear in the browser.

Whether you prefer this workaround to your own is probably a matter of taste. In this particular case, I think the css-based solution will be better, but it will depend on which browsers you need to support.

+3
source

The menu should be a list, so make it unordered and CSS:

 .menu ul li:after { content: '|'; } 
+1
source

I think you need to avoid this character. Try "\ |".

0
source

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


All Articles