2012-07-30 55 views
0

我使用HAML,和下面的代碼工作:HAML無法正確解析我的Ruby代碼

content = '= User.last.name' #last.name = 'joe' 
Haml::Engine.new(content).render >> joe 

爲什麼HAML正確地分析字符串,但無法正確解析以下字符串?

content = '= User.last.name 
       %title 
        html2haml and multiline titles' 

Haml::Engine.xxxx(content) >> "= User.last.name 
           <title> 
            html2haml and multiline titles 
           </title>" 

回答

0

我不清楚,如果您是按照上面的方式逐字輸入的,我認爲這會導致縮進問題。

考慮一個herestring:

content = <<EOS 
= User.last.name 
    %title 
    html2haml and multiline titles 
EOS 

我的猜測是你的字符串有缺口的問題;上述工作正常。

(有一個strip_heredoc in Rails 3這樣你就可以縮進herestring,其內容源更容易。)