Haml Getting Started

Source: Internet
Author: User

1. What is Haml

Haml is the HTML Abstraction Markup Language, followed by the principle that the tag should be beautiful. Haml can accelerate and simplify the template, the advantages are concise, readable, efficient.

Comparison of 2.ERBM templates and Haml templates

. Erb Template Code:

<section  class  = "container" ;  <h1 ;  <%= Post.title%>  </h1 ;  << Span class= "Hljs-title" >h2 ;  <%= post.subtitle%>  </h2 ;  < div  class  = "content ";  <%= post.content%>  </div ;  </section  > 

The same code uses HAML:

%section.container  %h1= post.title  %h2= post.subtitle  .content    = post.content
3. Installing Haml

Haml is a command-line tool, the Gem installation is expressly:

install  haml

Install the latest version:

geminstallhaml--pre

Update Gemfile in the Rails project to add Haml dependencies:

‘haml‘
4.erb Turn Haml

Haml is a substitute for the Erb, app/views. erb files can be changed directly to the Haml template by modifying the suffix name:

app/views/account/login.html.erb → app/views/account/login.html.haml
5. Using Haml 5.1 Erb Code transfer HAML code

ERB:

<strong><%= item.title %></strong>

Haml:

%strong= item.title

An HTML tag is represented in Haml by a% spike signature, for example,,,, %strong %div %body %html tag name followed by , tells Haml to calculate Ruby code, return value as the content of the label. The Haml automatically detects the newline character of the return value and formats the label.

5.2 Adding attributes to Tags:

Html:

<strong class="code" id="message">Hello, World!</strong>

HAML:

%strong{:class => "code", :id => "message"} Hello, World!
5.3 Simplifying Div

Html:

<div class=‘content‘>Hello, World!</div>

Haml:

.content Hello, World!
5.5 Example One

ERB:

<divclass=‘itemid=‘itemitem.id %>‘>  item.body %></div>

HAML:

.item{:id =>"item#{item.id}"item.body
5.2 Example 2

ERB:

<div id=' content '>  <div class=' left column '>    <h2>Welcome to our site!</H2>    <p><%= print_information%></P>  </div>  <div class="right column">    <%= render:p artial = "sidebar" %>  </div></div>

HAML:

#content  .left column     %h2 Welcome to our site!     %p  = print_information  .right column     =render :partial => "sidebar"

Haml using indentation to represent hierarchical relationships

Haml Getting Started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.