How can I write “Text” only once and at the same time check if path_info contains “A”?

- if !request.path_info.include? 'A'
  %{:id => 'A'}
   "Text"
- else
  "Text"

"Text" is recorded twice. How to do this in order to write it only once and at the same time check if path_info 'A' is included?

+5
source share
5 answers

There are two ways to do this. Using partial or using content_for block :

If the “Text” was longer or was a significant subtree, you could extract it in partial. This will slightly reduce your code. In the above example, this may seem redundant.

The best way in this case would be to use a content_for block, for example:

- if !request.path_info.include? 'A'
  %{:id => 'A'}
    =yield :content
- else
  =yield :content

-content_for :content do
  Text

content_for , "". , .

+4

, constuct:

%{:id => ('A' if request.path_info.include? 'A')}
  "Text"
+2

?

- text = "Text"     
- if !request.path_info.include? 'A'
  %div{:id => 'A'}
    = text
- else
  = text
+2

, HTML HAML. :

- if !request.path_info.include? 'A'
  <div id="A">
Text
- if !request.path_info.include? 'A'
  </div>

, Text - , div, DRY.

+1

: . HAML - , , , , .

+1
source

All Articles