###Yaml and HAML
What is YAML and HAML?
What does YAML stand for??
YAML: YAML Ain't Markup Language (Yet Another Markup Language) it was changed to distinguish its purpose as data-oriented and rather than a document markup
- YAML was created to easily map data types such as lists and arrays.
- YAML is structured in a way that is easy for human to read, like an outline
- YAML doesn't use enclosures such as quotation marks, brackets, braces and open/close tags.
- spaces and indentations are not important as long as parallel elements have the same left justification and nested elements are indented further
receipt: Oz-Ware Purchase Invoice
date: 2012-08-06
customer:
given: Dorothy
family: Gale
items:
- part_no: A4786
descrip: Water Bucket (Filled)
price: 1.47
quantity: 4
- part_no: E1628
descrip: High Heeled "Ruby" Slippers
size: 8
price: 100.27
quantity: 1
- so how do you use YAML with Ruby?
- !ruby/object:Person
name: John Doe
sname: jdoe
email: jdoe@gmail.com
- !ruby/object:Person
name: Jane Doe
sname: jdoe
email: jane@hotmail.com
FILENAME = 'data.yaml'
class Person
attr_accessor :name, :sname, :email
end
require "yaml"
# Will return an array of Person objects.
data = YAML::load(File.open(FILENAME))
# Will print out the first object in the array's name. #=> John Doe
puts data.first.name ==> "John Doe"
What does HAML stand for?
HAML: HTML Abstraction Markup Language.
Haml believes that markup should be beautiful and it should accelerate and simplify template creation.
- to write a tag in HAML we use the percent sign %
- if you are writing ruby code, after the percent sign would come a = which ells HAML to evaluate Ruby code to the right and print out the return value as the contents of the tag, like erb tags <%= %>
- you add attributes such as class, ids in HAML like how you would write a hash {:class => 'code'}
- since classes and ids are so common you can actually just write %strong.code#message and haml will understand you
- since divs are so common, you can actually leave them off too
- Nesting in HAML is taking care of by whitespace, so indentation
<strong><%= item.title %></strong>
<strong class="code" id="message">Hello, World!</strong>
%strong= item.title
%strong{:class => "code", :id => "message"} Hello, World!
%strong.code#message Hello, World!
I think...I'm going to embrace HAML
<div id='content'>
<div class='left column'>
<h2>Welcome to our site!</h2>
<p><%= print_information %></p>
</div>
<div class="right column">
<%= render :partial => "sidebar" %>
</div>
</div>
#content
.left.column
%h2 Welcome to our site!
%p= print_information
.right.column
= render :partial => "sidebar"