Haml and Sass: Solution for you who get tired of ugly markup

52
Eiwa System Management, Inc. / Nihon Haml-no-kai Keita Urashima Haml and Sass Solution for you who get tired of ugly markup

description

in RubyKaigi2009

Transcript of Haml and Sass: Solution for you who get tired of ugly markup

Page 1: Haml and Sass: Solution for you who get tired of ugly markup

Eiwa System Management, Inc. / Nihon Haml-no-kai

Keita Urashima

Haml and SassSolution for you who get tired of ugly markup

Page 2: Haml and Sass: Solution for you who get tired of ugly markup

•A programmer of Eiwa System Management, Inc.I practice agile development with Ruby and Rails.http://ruby.agile.esm.co.jp

• I like Vim, Gentoo Linux, Tiling Window Manager, Kinesis Contoured Keyboard, and Ruby (Of course).

Keita Urashima (ursm)http://twitter.com/ursm

Page 3: Haml and Sass: Solution for you who get tired of ugly markup
Page 4: Haml and Sass: Solution for you who get tired of ugly markup

Summary•The time of writing RHTML and CSS by hand was over.•You should use Haml and Sass.

Page 5: Haml and Sass: Solution for you who get tired of ugly markup

Don't you think so?•When developing Web Application...•Coding is fun. Ruby makes me feel good. ;)•However, neither (R)HTML nor CSS are so. :(

Page 6: Haml and Sass: Solution for you who get tired of ugly markup

Solution

Page 7: Haml and Sass: Solution for you who get tired of ugly markup

About Haml/Sass•Are libraries to generate HTML and CSS respectively.•Integrating with various Web Application Frameworks.e.g. Rails, Merb, Ramaze, Sinatra, etc...

•Originally created by Hampton Catlin. Currently maintained by Nathan Weizenbaum (nex3).

Page 8: Haml and Sass: Solution for you who get tired of ugly markup

http://nex-3.com/posts/84-haml-sass-2-2-released

Version 2.2 Released!

Page 9: Haml and Sass: Solution for you who get tired of ugly markup
Page 10: Haml and Sass: Solution for you who get tired of ugly markup

Problem•HTML is too redundant. It’s painful to write and to read it. •ERB is general purpose, and support to a specific format (e.g. HTML) is not offered.

Page 11: Haml and Sass: Solution for you who get tired of ugly markup

Solution

Page 12: Haml and Sass: Solution for you who get tired of ugly markup

What’s Haml?“Haml is a markup language that’s used to cleanly and simply describe the HTML of any web document without the use of inline code.”

- #haml.about (http://haml-lang.com/about.html)

Page 13: Haml and Sass: Solution for you who get tired of ugly markup

<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns='http://www.w3.org/1999/xhtml'>  <head>    <title><%=h @title %></title>  </head>  <body>    <% if @content_for_nav %>      <div class='navigation'>        <%= yield :nav %>      </div>    <% end %>    <div class='content'>      <%= yield %>    </div>  </body></html>

RHTML

Page 14: Haml and Sass: Solution for you who get tired of ugly markup

!!! XML!!! 1.1%html{:xmlns => 'http://www.w3.org/1999/xhtml'}  %head    %title&= @title  %body    - if @content_for_nav      .navigation        = yield :nav    .content      = yield

Haml

Page 15: Haml and Sass: Solution for you who get tired of ugly markup

192 characters432 characters

-55%

<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns='http://www.w3.org/1999/xhtml'>  <head>    <title><%=h @title %></title>  </head>  <body>    <% if @content_for_nav %>      <div class='navigation'>        <%= yield :nav %>      </div>    <% end %>    <div class='content'>      <%= yield %>    </div>  </body></html>

!!! XML!!! 1.1%html{:xmlns => 'http://www.w3.org/1999/xhtml'}  %head    %title&= @title  %body    - if @content_for_nav      .navigation        = yield :nav    .content      = yield

Page 16: Haml and Sass: Solution for you who get tired of ugly markup

Core Principles•Markup should be beautiful•Markup should be DRY•Markup should be well-indented•HTML structure should be clear

Page 17: Haml and Sass: Solution for you who get tired of ugly markup

How?•Haml is specialized in a HTML, and has many functions for that.

Let’s see it.

Page 18: Haml and Sass: Solution for you who get tired of ugly markup

RHTML Haml

<h1>Hello, World!</h1> %h1 Hello, World!

Elements

Page 19: Haml and Sass: Solution for you who get tired of ugly markup

RHTML Haml<p> Hello, World!</p>

%p Hello, World!

<ul> <li>alpha <ul> <li>bravo</li> </ul> </li> <li>charly</li></ul>

%ul %li alpha %ul %li bravo %li charly

Elements

Page 20: Haml and Sass: Solution for you who get tired of ugly markup

AttributesRHTML Haml

<a href=”…”>Haml</a> %a{:href => ’…’} Haml

<div class=”block”> …</div>

%div{:class=>‘block’} …

%div.block …

.block …

<div id=”header”> …</div>

#header …

Page 21: Haml and Sass: Solution for you who get tired of ugly markup

Ruby evaluationRHTML Haml

<%= link_to … %> = link_to …

<p> <%=h user.name %></p>

%p&= user.name

<li title=”<%= user.name %>”> …</li>

%li{:title=>user.name} …

Page 22: Haml and Sass: Solution for you who get tired of ugly markup

Ruby evaluation

RHTML Haml

<% users.each do |u| %><li><%=h u.name %></li><% end %>

- users.each do |u| %li&= u.name

<% if @user.admin? %> <%= admin_menu %><% else %> <%= common_menu %><% end %>

- if @user.admin? = admin_menu- else = common_menu

Page 23: Haml and Sass: Solution for you who get tired of ugly markup

- %w(foo bar).each do |s| = s- end

Syntax error on line 3: You don't need to use "- end" in Haml. Use indentation instead:- if foo? %strong Foo!- else Not foo.

Page 24: Haml and Sass: Solution for you who get tired of ugly markup

= %w(foo bar).map do |s| - s.upcase- end.join(‘<br/>’)

<%= %w(foo bar).map do |s| %> <% s.upcase %><% end.join(‘<br/>’) %>

In Haml 2.2:

Page 25: Haml and Sass: Solution for you who get tired of ugly markup

Doctype/XML Declaration

RHTML Haml<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" …>

!!!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" …>

!!! 1.1

<?xml version='1.0' encoding='utf-8' ?> !!! XML

Page 26: Haml and Sass: Solution for you who get tired of ugly markup

!!! XML!!! 1.1%html{:xmlns => 'http://www.w3.org/1999/xhtml'}  %head    %title&= @title  %body    - if @content_for_nav      .navigation        = yield :nav    .content      = yield

Page 27: Haml and Sass: Solution for you who get tired of ugly markup

Advantage of Haml•Haml removes the redundancy of RHTML.•This means improvement of writability and readability.

Page 28: Haml and Sass: Solution for you who get tired of ugly markup
Page 29: Haml and Sass: Solution for you who get tired of ugly markup

Problem•CSS lacks the functions for making maintenance easy.

Page 30: Haml and Sass: Solution for you who get tired of ugly markup

Solution

Page 31: Haml and Sass: Solution for you who get tired of ugly markup

What’s Sass?“Sass is a meta-language on top of CSS that’s used to describe the style of a document cleanly and structurally, with more power than flat CSS allows.”

Page 32: Haml and Sass: Solution for you who get tired of ugly markup

Nested Rules•In CSS, all rules are described in flat.•Sass allows rules which nested.

Page 33: Haml and Sass: Solution for you who get tired of ugly markup

CSS Sass.content { margin: 1em;}.content h1 { font-size: 3em;}.content h1 strong { color: red;}

.content margin: 1em

h1 font-size: 1em

strong color: red

Page 34: Haml and Sass: Solution for you who get tired of ugly markup

Variables and Operations•In CSS, you have to write the same value repeatedly.•You can use variable with Sass.

Page 35: Haml and Sass: Solution for you who get tired of ugly markup

!base_color = red!base_margin = 3em

h3 color: white background-color = !base_color margin-left = !base_margin

Page 36: Haml and Sass: Solution for you who get tired of ugly markup

!base_color = red!base_margin = 3em

h4 color = !base_color - #666 background-color: white margin-left = !base_margin + 1em

Page 37: Haml and Sass: Solution for you who get tired of ugly markup

Mixins•CSS doesn’t have reusability.•Sass can reuse collection of are rules.

Page 38: Haml and Sass: Solution for you who get tired of ugly markup

=round-border border-radius: 16px -webkit-border-radius: 16px -moz-border-radius: 16px

.navigation a +round-border

.navigation a { border-radius: 16px; -webkit-border-radius: 16px; -moz-border-radius: 16px;}

Page 39: Haml and Sass: Solution for you who get tired of ugly markup

=clearfix display: inline-block &:after content: "." display: block height: 0 clear: both visibility: hidden * html & height: 1px

ul.menu +clearfix li float: left

Page 40: Haml and Sass: Solution for you who get tired of ugly markup

=sexy-border(!color, !width = 1in) border: color = !color width = !width style: dashed

p +sexy-border("blue")

p { border-color: #0000ff; border-width: 1in; border-style: dashed;}

Page 41: Haml and Sass: Solution for you who get tired of ugly markup

Advantage of Sass•Sass brings power to CSS. Stylesheet for a programmer.

Page 42: Haml and Sass: Solution for you who get tired of ugly markup

Can these be used really?•These were made for professional use. It’s actually used in some companies. •These have been developed actively, and the bug is corrected promptly.•I used Haml/Sass from one year ago. Any problems didn’t occur.

Page 43: Haml and Sass: Solution for you who get tired of ugly markup

Getting Started

$ gem install haml

http://haml-lang.com/help.htmlhttp://sass-lang.com/help.html

Page 44: Haml and Sass: Solution for you who get tired of ugly markup

Integration with Rails1. Install a plugin$ haml --rails path/to/rails_app

2. Put your template and stylesheet$ vim app/views/hello/index.html.haml$ vim public/stylesheets/sass/style.sass

3. Run your application

Page 45: Haml and Sass: Solution for you who get tired of ugly markup

Summary•The time of writing RHTML and CSS by hand was over.•You should use Haml and Sass.

Page 46: Haml and Sass: Solution for you who get tired of ugly markup

•I work with a designer. Can we use Haml/Sass?•I have already used RHTML. How should I migrate to Haml?•How about the performance of Haml?

Expected Q&A

Page 47: Haml and Sass: Solution for you who get tired of ugly markup

I work with a designer.Can we use Haml/Sass?

•I haven’t tried it. I think that it’s difficult.•If you are one team, it might be possible.

Page 48: Haml and Sass: Solution for you who get tired of ugly markup

I have already used RHTML.How should I migrate to Haml?

•It isn’t necessary to do all at once. You can migrate every one file and one partial.

Page 49: Haml and Sass: Solution for you who get tired of ugly markup

How about the performance of Haml?

•You should ask kwatch.•See http://nex-3.com/posts/81-more-haml-benchmark-issues“According to my benchmarks, master is 2.54 times slower than ERB by default, but is in fact 2% faster than ERB when the :ugly option is enabled.”

Page 50: Haml and Sass: Solution for you who get tired of ugly markup
Page 51: Haml and Sass: Solution for you who get tired of ugly markup

Thank You!

Page 52: Haml and Sass: Solution for you who get tired of ugly markup

Any Questions?