Back

【笔记】 使用HAML 替换 ERB 的步骤

发布时间: 2012-03-28 14:57:00

Steps of replacing erb with haml

 

1.  最最重要的: HAML 的注释:  -#  。可以在替换的时候,先把原来的代码加成注释。 然后再慢慢改~~~

2.  HAML 模板中,不允许出现HTML 等等代码,所以建议先把全文都 -#, 然后再一点一点改。

3.  HAML is more a structure language, 所以, :

 

  erb:   <p> This <%= technology %> is <%= a_littel %>  funcy, with <%= somehow... %> shorthand</p>

 

  ==  

  haml:   

  %p "This #{technology} is #{a_littel} functy..."

  === 注意! 下面的办法不推荐。

  改成HAML 就得是(大概吧,以我目前的理解,估计再找找文档应该有解决方案)

  HAML: 

  %p This

    = technology

    is

    = a_littel

    funcy, with

    = somehow

    shorthand

 

 

 OK,下面开始 记录我的HAML 替换 ERB 的步骤:

 

 div id = 'ooxx'         =>      #ooxx

 

 div id='ooxx' class='enen'  =>  #ooxx.enen

 

 ul  => %ul

 

 div style ="abc"   =>  %div { :style => "abc"}

 

 HTML 注释: 

 <!--  comments -->    =>    / comments

 

 执行RUBY 代码:

 

 <% if true %> 

    <p>123456</p>

 <% end %>

 

变成:  (没有 end ,通过缩进来实现BLOCK的结束)

  - if true

    %p 123456

 

<%= current_user.name %>   =>    =current_user.name

 

javascript:

 

<script>  alert('hi') </script>

 

:javascript

  alert('hi')

 

HAML 注释要跟上面的缩进一致。 否则会出现:

Illegal nesting: content can't be both given on the same line as %div and nested within it.

a. 正确的:

#some_id.some_class Hello moto!

-# some code here....

 

b. 错误的:

#some_id.some_class Hello moto!

  -# some code here....   (注意这一行有缩进)

 

 

 

A line of Ruby code can be stretched over multiple lines as long as each line but the last ends with a comma. For example:

= link_to_remote "Add to cart",
    :url => { :action => "add", :id => product.id },
    :update => { :success => "cart", :failure => "error" }

 

Attribute hashes can also be stretched out over multiple lines to accommodate many attributes. However, newlines may only be placed immediately after commas. For example:

%script{:type => "text/javascript",
        :src  => "javascripts/script_#{2 + 7}"}

Back