Search code examples
haml

haml using variable in rails layout for meta keywords


We would like to adjust the meta keywords of a web page based upon the specific action. As a novice at haml, is there a way that this could be done via a provides statement. For example:

in client.html.haml

- provide(:keywords, 'here are unique keywords')

and in our application.html.haml, how would we do this? Like:

%meta{ name: "keywords", content:  #{yield(:keywords)} ||= default keywords

but this doesn't work.


Solution

  • You don't say why your approach doesn't work, so I'm going to guess it's a syntax error in your haml (missing closing bracket). Also, I'm not sure what default keywords is or why you're using the ||= operator - either of those may also be causing your solution to "not work".

    Something like the following will work as expected:

    # application.html.haml
    %meta{ name: "keywords", |
           content: content_for?(:keywords) ? yield(:keywords) : "default" }
           # this could also be achieved using content_for?(:keywords) || "default"
    
    # client.html.haml
    - provide :keywords do
      this should work