1

I'd like to add same content but assign a different tag in haml for the sake of SEO. More specifically, I want the header to be <h1> in PostsController#index but to be <p> on other pages so I try to use

-if current_page?(controller: 'top', action: 'index') ? %h2 , %h3

but it doesn't work and I think the wrong part in %h2 %h3 part.

Kittichote Chain
  • 596
  • 1
  • 10
  • 22
  • the ternary does not need a leading `if` otherwise this is interpreted as `if(if(current_page?(controller: 'top', action: 'index')) then %h2 else %h3)` which in turn just evaluates to what is essentially `if(true)`. Additionally ternary syntax is `condition ? true_value : false_value` Note (**`:`** not comma). I am not a haml expert but [This SO Post](https://stackoverflow.com/a/12331309/1978251) should get you the rest of the way there. – engineersmnky Jul 03 '17 at 13:50

2 Answers2

1

I'm not that familiar with HAML but perhaps this?

- if current_page?(controller: 'top', action: 'index')
  %h2= some_variable_or_text
- else 
  %p= some_variable_or_text
Ho Man
  • 2,308
  • 11
  • 16
1

You can use the haml_tag helper, something like this:

- haml_tag current_page?(controller: 'top', action: 'index') ? :h2 : :h3 do
  Content here.

Note that the helper arguments are symbols, using : not %.

matt
  • 78,533
  • 8
  • 163
  • 197