1
0
Fork 0

Slim, Liquid templates

This commit is contained in:
Gregory Eremin 2013-12-14 02:40:54 +07:00
parent b41d194a63
commit 41d073b785
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
12 changed files with 171 additions and 40 deletions

View File

@ -5,3 +5,5 @@ gem 'tilt'
gem 'erubis' gem 'erubis'
gem 'haml' gem 'haml'
gem 'slim'
gem 'liquid'

View File

@ -7,6 +7,11 @@ GEM
haml (4.0.4) haml (4.0.4)
tilt tilt
i18n (0.6.9) i18n (0.6.9)
liquid (2.6.0)
slim (2.0.2)
temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1)
temple (0.6.7)
tilt (2.0.0) tilt (2.0.0)
PLATFORMS PLATFORMS
@ -16,4 +21,6 @@ DEPENDENCIES
erubis erubis
faker faker
haml haml
liquid
slim
tilt tilt

View File

@ -8,7 +8,7 @@ cd template_engine_comparison
bundle install bundle install
# Generate data for templates # Generate data for templates
./fakedata.rb 10000 > data/big.yml ./fakedata.rb 1000 > data/big.yml
# Run benchmarks # Run benchmarks
./benchmark.rb ./benchmark.rb
@ -17,18 +17,42 @@ bundle install
## Current Results ## Current Results
``` ```
############################################################ ############################################################
## Compilation (1000 runs) ## ## Compilation (small) (1000 runs) ##
############################################################ ############################################################
user system total real user system total real
erb 0.230000 0.020000 0.250000 ( 0.247248) erb 0.250000 0.030000 0.280000 ( 0.275828)
erubis 0.050000 0.010000 0.060000 ( 0.061928) erubis 0.060000 0.020000 0.080000 ( 0.077510)
haml 0.880000 0.050000 0.930000 ( 0.935885) haml 0.860000 0.030000 0.890000 ( 0.892039)
slim 1.960000 0.030000 1.990000 ( 1.989972)
liquid 0.060000 0.020000 0.080000 ( 0.074439)
############################################################ ############################################################
## Render (10000 runs) ## ## Render (small) (1000 runs) ##
############################################################ ############################################################
user system total real user system total real
erb 0.140000 0.000000 0.140000 ( 0.136514) erb 0.010000 0.000000 0.010000 ( 0.012331)
erubis 0.080000 0.000000 0.080000 ( 0.085103) erubis 0.010000 0.000000 0.010000 ( 0.008456)
haml 0.250000 0.020000 0.270000 ( 0.263665) haml 0.020000 0.000000 0.020000 ( 0.027852)
slim 0.030000 0.000000 0.030000 ( 0.021398)
liquid 0.030000 0.000000 0.030000 ( 0.037635)
############################################################
## Compilation (big) (1000 runs) ##
############################################################
user system total real
erb 0.330000 0.020000 0.350000 ( 0.352752)
erubis 0.120000 0.020000 0.140000 ( 0.139981)
haml 1.200000 0.030000 1.230000 ( 1.226896)
slim 2.440000 0.030000 2.470000 ( 2.469225)
liquid 0.120000 0.020000 0.140000 ( 0.133451)
############################################################
## Render (big) (100 runs) ##
############################################################
user system total real
erb 0.650000 0.020000 0.670000 ( 0.667166)
erubis 0.430000 0.010000 0.440000 ( 0.446348)
haml 0.970000 0.020000 0.990000 ( 0.993755)
slim 1.440000 0.010000 1.450000 ( 1.445460)
liquid 10.660000 0.050000 10.710000 ( 10.715820)
``` ```

View File

@ -4,12 +4,16 @@ require 'rubygems'
require 'bundler/setup' require 'bundler/setup'
require 'benchmark' require 'benchmark'
require 'yaml' require 'yaml'
require 'ostruct'
require 'erubis' require 'erubis'
require 'haml' require 'haml'
require 'slim'
require 'liquid'
require 'tilt/erb' require 'tilt/erb'
require 'tilt/erubis' require 'tilt/erubis'
require 'tilt/haml' require 'tilt/haml'
require 'tilt/liquid'
# Terminal settings # Terminal settings
TERMINAL_WIDTH = 60 TERMINAL_WIDTH = 60
@ -59,6 +63,14 @@ ENGINES = {
haml: { haml: {
class: Tilt::HamlTemplate, class: Tilt::HamlTemplate,
extension: 'haml' extension: 'haml'
},
slim: {
class: Slim::Template,
extension: 'slim'
},
liquid: {
class: Tilt::LiquidTemplate,
extension: 'liquid'
} }
} }
@ -72,8 +84,13 @@ puts
banner 'Render (small) (%d runs)' % RENDER_LOOPS banner 'Render (small) (%d runs)' % RENDER_LOOPS
benchmark(loops: RENDER_LOOPS) do |name, attrs| benchmark(loops: RENDER_LOOPS) do |name, attrs|
case name
when :slim
templates[name].render(OpenStruct.new(small_data))
else
templates[name].render(Object, small_data) templates[name].render(Object, small_data)
end end
end
puts puts
banner 'Compilation (big) (%d runs)' % COMPILE_LOOPS banner 'Compilation (big) (%d runs)' % COMPILE_LOOPS
@ -83,8 +100,13 @@ benchmark(loops: COMPILE_LOOPS) do |name, attrs|
end end
puts puts
banner 'Render (big) (%d runs)' % RENDER_LOOPS banner 'Render (big) (%d runs)' % 100
benchmark(loops: 100) do |name, attrs| benchmark(loops: 100) do |name, attrs|
templates[name].render(Object, customers: big_data) case name
when :slim
templates[name].render(OpenStruct.new(customers: big_data))
else
templates[name].render(Object, {customers: big_data})
end
end end
puts puts

View File

@ -13,13 +13,13 @@ end
data = 1.upto(num_records).map do data = 1.upto(num_records).map do
{ {
name: Faker::Name.name, 'name' => Faker::Name.name,
age: rand(20..50), 'age' => rand(20..50),
country: Faker::Address.country, 'country' => Faker::Address.country,
city: Faker::Address.city, 'city' => Faker::Address.city,
zip: Faker::Address.zip.to_i, 'zip' => Faker::Address.zip.to_i,
address: Faker::Address.street_address(true), 'address' => Faker::Address.street_address(true),
employer: Faker::Company.name 'employer' => Faker::Company.name
} }
end end

View File

@ -7,18 +7,18 @@
<% customers.each do |customer| %> <% customers.each do |customer| %>
<dl> <dl>
<dt>Name:</dt> <dt>Name:</dt>
<dd><%= customer[:name] %></dd> <dd><%= customer['name'] %></dd>
<dt>Age:</dt> <dt>Age:</dt>
<dd><%= customer[:age] %></dd> <dd><%= customer['age'] %></dd>
<dt>Address:</dt> <dt>Address:</dt>
<dd> <dd>
<%= customer[:zip] %> <%= customer['zip'] %>
<%= customer[:country] %> <%= customer['country'] %>
<%= customer[:city] %> <%= customer['city'] %>
<%= customer[:address] %> <%= customer['address'] %>
</dd> </dd>
<dt>Employer:</dt> <dt>Employer:</dt>
<dd><%= customer[:employer] %></dd> <dd><%= customer['employer'] %></dd>
</dl> </dl>
<% end %> <% end %>
</body> </body>

View File

@ -7,18 +7,18 @@
<% customers.each do |customer| %> <% customers.each do |customer| %>
<dl> <dl>
<dt>Name:</dt> <dt>Name:</dt>
<dd><%= customer[:name] %></dd> <dd><%= customer['name'] %></dd>
<dt>Age:</dt> <dt>Age:</dt>
<dd><%= customer[:age] %></dd> <dd><%= customer['age'] %></dd>
<dt>Address:</dt> <dt>Address:</dt>
<dd> <dd>
<%= customer[:zip] %> <%= customer['zip'] %>
<%= customer[:country] %> <%= customer['country'] %>
<%= customer[:city] %> <%= customer['city'] %>
<%= customer[:address] %> <%= customer['address'] %>
</dd> </dd>
<dt>Employer:</dt> <dt>Employer:</dt>
<dd><%= customer[:employer] %></dd> <dd><%= customer['employer'] %></dd>
</dl> </dl>
<% end %> <% end %>
</body> </body>

View File

@ -6,14 +6,14 @@
- customers.each do |customer| - customers.each do |customer|
%dl %dl
%dt Name: %dt Name:
%dd= customer[:name] %dd= customer['name']
%dt Age: %dt Age:
%dd= customer[:age] %dd= customer['age']
%dt Address: %dt Address:
%dd %dd
= customer[:zip] = customer['zip']
= customer[:country] = customer['country']
= customer[:city] = customer['city']
= customer[:address] = customer['address']
%dt Employer: %dt Employer:
%dd= customer[:employer] %dd= customer['employer']

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Customers List</title>
</head>
<body>
{% for customer in customers %}
<dl>
<dt>Name:</dt>
<dd>{{ customer.name }}</dd>
<dt>Age:</dt>
<dd>{{ customer.age }}</dd>
<dt>Address:</dt>
<dd>
{{ customer.zip }}
{{ customer.country }}
{{ customer.city }}
{{ customer.address }}
</dd>
<dt>Employer:</dt>
<dd>{{ customer.employer }}</dd>
</dl>
{% endfor %}
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello, {{ username }}!</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</p>
</body>
</html>

19
templates/slim/big.slim Normal file
View File

@ -0,0 +1,19 @@
doctype html
html
head
title Customers List
body
- customers.each do |customer|
dl
dt Name:
dd = customer['name']
dt Age:
dd = customer['age']
dt Address:
dd
== customer['zip']
== customer['country']
== customer['city']
== customer['address']
dt Employer:
dd = customer['employer']

14
templates/slim/small.slim Normal file
View File

@ -0,0 +1,14 @@
doctype html
html
head
title = title
body
h1 Hello, #{username}!
p
| Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
| tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
| veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
| commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
| velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
| occaecat cupidatat non proident, sunt in culpa qui officia deserunt
| mollit anim id est laborum.