Haml
Haml缩写自HTML Abstraction Markup Language。这又是一次懒人们对笨重臃肿的死缠烂打。
基本规则
在Rails项目中*.html.haml
将会使用haml进行转换。替换规则如下。
%strong => <strong></strong>
%strong.classname => class="classname"
%strong#id => id="id"
= print_information => insert ruby
- 10.each do |i| => running ruby
:markdown => filter, use plugin process current text
对比示例
<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"
{} - hash类型属性
<div class='item' id='item<%= item.id %>'>
<%= item.body %>
</div>
.item{:id => "item#{item.id}"}= item.body
%div{:id => [@item.type, @item.number], :class => [@item.type, @item.urgency]}
# equal to
%div{:id => "#{@item.type}_#{@item.number}", :class => "#{@item.type} #{@item.urgency}"}
() - 文本类型属性
%script(type="text/javascript"
src="javascripts/script_#{2 + 7}")
<script src='javascripts/script_9' type='text/javascript'></script>
: - Filters
%p
:markdown
# Greetings
Hello, *World*
<p>
<h1>Greetings</h1>
<p>Hello, <em>World</em></p>
</p>
Slim
Slim是对haml的进一步简化
基本规则
使用缩进定义块,标签名字定义可简写
- => control code
= => output code
== => output without html escape
* => convert hash to key value pairs
meta => directly as tag name
实例对比
.card*{'data-url'=>place_path(place), 'data-id'=>place.id} = place.name
.card *@hash_instance_variable = place.name
<div class="card" data-id="1234" data-url="/place/1234">Slim's house</div>
ruby:
def a_unless_current
@page_current ? {:tag => 'span'} : {:tag => 'a', :href => 'http://slim-lang.com/'}
end
- @page_current = true
*a_unless_current Link
- @page_current = false
*a_unless_current Link
<span>Link</span><a href="http://slim-lang.com/">Link</a>
doctype html
html
head
title Slim Examples
meta name="keywords" content="template language"
meta name="author" content=author
link rel="icon" type="image/png" href=file_path("favicon.png")
javascript:
alert('Slim supports embedded javascript!')
body
h1 Markup examples
#content
p This example shows you how a basic Slim file looks.
== yield
- if items.any?
table#items
- for item in items
tr
td.name = item.name
td.price = item.price
- else
p No items found Please add some inventory.
Thank you!
div id="footer"
== render 'footer'
| Copyright © #{@year} #{@author}
性能测试
# Linux + Ruby 1.9.2, 1000 iterations
user system total real
(1) erb 0.680000 0.000000 0.680000 ( 0.810375)
(1) erubis 0.510000 0.000000 0.510000 ( 0.547548)
(1) fast erubis 0.530000 0.000000 0.530000 ( 0.583134)
(1) slim 4.330000 0.020000 4.350000 ( 4.495633)
(1) haml 4.680000 0.020000 4.700000 ( 4.747019)
(1) haml ugly 4.530000 0.020000 4.550000 ( 4.592425)
(2) erb 0.240000 0.000000 0.240000 ( 0.235896)
(2) erubis 0.180000 0.000000 0.180000 ( 0.185349)
(2) fast erubis 0.150000 0.000000 0.150000 ( 0.154970)
(2) slim 0.050000 0.000000 0.050000 ( 0.046685)
(2) haml 0.490000 0.000000 0.490000 ( 0.497864)
(2) haml ugly 0.420000 0.000000 0.420000 ( 0.428596)
(3) erb 0.030000 0.000000 0.030000 ( 0.033979)
(3) erubis 0.030000 0.000000 0.030000 ( 0.030705)
(3) fast erubis 0.040000 0.000000 0.040000 ( 0.035229)
(3) slim 0.040000 0.000000 0.040000 ( 0.036249)
(3) haml 0.160000 0.000000 0.160000 ( 0.165024)
(3) haml ugly 0.150000 0.000000 0.150000 ( 0.146130)
(4) erb 0.060000 0.000000 0.060000 ( 0.059847)
(4) erubis 0.040000 0.000000 0.040000 ( 0.040770)
(4) slim 0.040000 0.000000 0.040000 ( 0.047389)
(4) haml 0.190000 0.000000 0.190000 ( 0.188837)
(4) haml ugly 0.170000 0.000000 0.170000 ( 0.175378)
1. Uncached benchmark. Template is parsed every time.
Activate this benchmark with slow=1.
2. Cached benchmark. Template is parsed before the benchmark.
The ruby code generated by the template engine might be evaluated every time.
This benchmark uses the standard API of the template engine.
3. Compiled benchmark. Template is parsed before the benchmark and
generated ruby code is compiled into a method.
This is the fastest evaluation strategy because it benchmarks
pure execution speed of the generated ruby code.
4. Compiled Tilt benchmark. Template is compiled with Tilt, which gives a more
accurate result of the performance in production mode in frameworks like
Sinatra, Ramaze and Camping. (Rails still uses its own template
compilation.)
详细参见 Haml: http://haml.info/docs/yardoc/file.REFERENCE.html Slim: http://rdoc.info/gems/slim/frames