简体   繁体   中英

Meta Descritpion in HAML with outside variable

I'm trying to get my meta description to work in HAML and everything I try produces errors.

%meta{:name => "description", :content => "Some content"}/
%title 
  = data.page.title

The code above works. Now I try the following:

 %meta{:name => "description", :content => 
   = data.page.desc
   }/
 %title 
   = data.page.title

And I get unbalanced brackets error on the first line. What am I doing wrong?

In HAML, the hash that you use to specify the attributes for an element can contain valid Ruby code, so you don't need to use = to evaluate a Ruby expression. Therefore, the code you're looking for is simply:

%meta{:name => "description", :content => data.page.desc}

Note that you don't need to append a / to the end of the %meta element declaration, as HAML will automatically treat it as a self-closing tag, like img or br .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM