skip to Main Content

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.

2

Answers


  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
    
    Login or Signup to reply.
  2. 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 %.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search