tilt-jadeite 0.0.2 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,60 +1,168 @@
1
- # Tilt::Jadeite
2
-
3
- Render Jade templates with help from therubyracer embedded V8 JavaScript interpreter.
4
-
5
- Also adds Jade support to Sinatra, i.e.:
6
-
7
- ```ruby
8
- get "/freakin" do
9
- jade 'p this is soo #{adjective.substring(0,5) + "in awesome"}', :locals => { :adjective => "freaky" }
10
- end
11
- ```
12
-
13
- ## Installation
1
+ ```
2
+ _ _ _ _ _
3
+ _ (_) | _ (_) | | (_) _
4
+ _| |_ _| |_| |_ _____ _ _____ __| |_____ _ _| |_ _____
5
+ (_ _) | (_ _|_____) (____ |/ _ | ___ | (_ _) ___ |
6
+ | |_| | | | |_ | / ___ ( (_| | ____| | | |_| ____|
7
+ \__)_|\_) \__) _| \_____|\____|_____)_| \__)_____)
8
+ (__/
9
+ ```
10
+ [Tilt](https://github.com/rtomayko/tilt) extension for [Jadeite](https://github.com/bengler/jadeite) that adds support for rendering
11
+ [Jade](http://jade-lang.com) templates from Sinatra with help from
12
+ [The Ruby Racer](https://github.com/cowboyd/therubyracer) embedded V8 JavaScript interpreter.
13
+
14
+ ```ruby
15
+ get "/bacon" do
16
+ jade "p Everyone loves #{what[~~(Math.random()*(what.length-1))]} bacon!", :locals => {
17
+ :what => ['smoked', 'tasty', 'delicious', 'chunky']
18
+ }
19
+ end
20
+ ```
21
+
22
+ ```
23
+ => <p>Everyone loves chunky bacon!</p>
24
+ ```
25
+
26
+ ### Sinatra example
27
+
28
+ #### mytemplate.jade
29
+
30
+ ```jade
31
+ doctype 5
32
+ html(lang="en")
33
+ head
34
+ title= pageTitle
35
+ script(type='text/javascript')
36
+ if (foo) {
37
+ bar()
38
+ }
39
+ body
40
+ h1 Jade - node template engine
41
+ #container
42
+ if youAreUsingJade
43
+ p You are amazing
44
+ else
45
+ p Get on it!
46
+ ```
47
+
48
+ #### app.rb
49
+
50
+ ```ruby
51
+ require "tilt-jadeite/sinatra"
52
+
53
+ class HelloWorld < Sinatra::Base
54
+ helpers Sinatra::Jade
55
+
56
+ get "/jade" do
57
+ jade :mytemplate, :locals => {:pageTitle => "Jade", :youAreUsingJade => true}
58
+ end
59
+ end
14
60
 
15
- Add this line to your application's Gemfile:
61
+ ```
62
+
63
+ Accessing your app at /jade will now produce
64
+ ```html
65
+ <!DOCTYPE html>
66
+ <html lang="en">
67
+ <head>
68
+ <title>Jade</title>
69
+ <script type="text/javascript">
70
+ if (foo) {
71
+ bar()
72
+ }
73
+ </script>
74
+ </head>
75
+ <body>
76
+ <h1>Jade - node template engine</h1>
77
+ <div id="container">
78
+ <p>You are amazing</p>
79
+ </div>
80
+ </body>
81
+ </html>
82
+ ```
83
+
84
+ ## Rendering server side with Sinatra
85
+
86
+ The goal of this library is to be able to use the same templates for rendering on server and in browser. Thus, it makes
87
+ little sense to include support for using Sinatra helper methods in the templates. Therefore, only the `locals` hash are
88
+ passed to the template engine. The keys of this hash will be the only variables/functions exposed to templates.
89
+
90
+ If you still wish to expose helper methods from the Sinatra class to your templates,
91
+ you can do this by adding the desired keys to the locals hash, enclosing methods in anonymous procs/lambdas.
92
+
93
+ As this example illustrates:
94
+
95
+ ```rb
96
+ get "/" do
97
+ jade :index, :locals => {
98
+ :stylesheet_path => proc {|_ignore, path| stylesheet_path(path) }
99
+ }
100
+ end
101
+ ```
16
102
 
17
- gem 'tilt-jadeite'
103
+ Now, the `stylesheet_path` _function_ is available in the template:
18
104
 
19
- And then execute:
105
+ ```jade
106
+ doctype 5
107
+ html
108
+ head
109
+ link(href=stylesheet_path('app'), rel="stylesheet", type="text/css")
110
+ link(href=stylesheet_path('mobile'), rel="stylesheet", type="text/css", media='screen and (min-width: 481px)')
111
+ // (...)
112
+ ```
20
113
 
21
- $ bundle
114
+ Remember that Jade is JavaScript, so the procs are converted to functions and thus require `()` after to be invoked.
22
115
 
23
- Or install it yourself as:
116
+ You can even expose the `haml` render method this way:
24
117
 
25
- $ gem install tilt-jadeite
118
+ ```ruby
119
+ get "/crazy" do
120
+ jade "!= haml('%p Mad crazy!')", :locals => { :haml => proc {|_ignore, tmpl| haml(tmpl) } }
121
+ end
26
122
 
27
- ## Usage
123
+ ```
28
124
 
29
- ### Sinatra
125
+ This will output:
126
+ ```html
127
+ <p>Mad crazy!</p>
128
+ ```
30
129
 
31
- #### hello.jade
130
+ Whether this is a good idea is another question! See performance considerations below.
32
131
 
33
- ```jade
34
- p Hello #{what}!
35
- ```
132
+ ## Performance
36
133
 
37
- #### hello_world.rb
134
+ The Ruby Racer performs data conversion when passing Ruby objects to the V8 context. This does not come for free
135
+ and can quickly result in performance problems if not taken into consideration. The solution is to keep the
136
+ `locals` hash as small and lightweight as possible. It is a good idea to calculate as much data as possible up front.
38
137
 
39
- ```ruby
40
- require "tilt-jadeite/sinatra"
138
+ I.e. an improved version of the `stylesheet_path` example above would determine the path to the needed stylesheets *before*
139
+ invoking the template engine:
41
140
 
42
- class HelloWorld < Sinatra::Base
43
- helpers Sinatra::Jade
141
+ ```
142
+ jade :index, :locals => {
143
+ :stylesheets => {
144
+ :app => stylesheet_path('app'),
145
+ :mobile => stylesheet_path('mobile')
146
+ }
147
+ }
148
+ ```
149
+ modifying the jade template to:
44
150
 
45
- get "/hello" do
46
- jade :hello, :what => "world"
47
- end
48
- end
151
+ ```jade
152
+ link(href=stylesheets.app, rel="stylesheet", type="text/css")
153
+ link(href=stylesheets.mobile, rel="stylesheet", type="text/css", media='screen and (min-width: 481px)')
154
+ // (...)
155
+ ```
49
156
 
50
- ```
157
+ ## Author
51
158
 
52
- Accessing your app at /hello will output `<p>Hello world!</p>`
159
+ Bjørge Næss / [github.com/bjoerge](https://github.com/bjoerge)
53
160
 
54
- ## Author
161
+ ## Credits
55
162
 
56
- Bjørge Næss <bjoerge@bengler.no>
163
+ All kudos in the world goes to TJ Holowaychuk for creating the eminent template language [Jade](http://jade-lang.com/) and to Charles Lowell for
164
+ the amazing [Ruby Racer](https://github.com/cowboyd/therubyracer).
57
165
 
58
- ## LICENSE
166
+ ## LICENSE
59
167
 
60
- MIT
168
+ MIT
@@ -0,0 +1,72 @@
1
+ require 'benchmark'
2
+ require 'tilt'
3
+ require '../lib/tilt-jadeite'
4
+
5
+ class Scope
6
+
7
+ def foo(what)
8
+ "Foo says #{what}"
9
+ end
10
+
11
+ def play(artist, song)
12
+ "Play #{song} from #{artist}"
13
+ end
14
+
15
+ def say_hello
16
+ "Hello to you!"
17
+ end
18
+
19
+ def fruity_song
20
+ "banana-na-na-na-na-na"
21
+ end
22
+
23
+ def development
24
+ true
25
+ end
26
+ end
27
+
28
+ proxied = Scope.new
29
+
30
+ data = {
31
+ :foo => proc { |what|
32
+ proxied.send(:foo, what)
33
+ },
34
+
35
+ :play => proc { |artist, song|
36
+ proxied.send(:play, artist, song)
37
+ },
38
+
39
+ :say_hello => "Hello World",
40
+ :fruity_song => proxied.fruity_song,
41
+ :development => proxied.development
42
+ }
43
+
44
+ FILES = %w(simple.jade template.jade template.haml template.erb large.jade large.haml large.erb)
45
+
46
+ def measure(times, title, &blk)
47
+ puts
48
+ puts "=== #{title} - #{times} times each"
49
+ FILES.each do |f|
50
+ puts "=> #{f}"
51
+ blk.call(times, f, Tilt[f])
52
+ puts
53
+ end
54
+ end
55
+
56
+ measure 1000, "Initialize + render" do |times, file, template_class|
57
+ puts Benchmark.measure {
58
+ times.times do
59
+ scope = Scope.new
60
+ template = template_class.new(file)
61
+ template.render(scope, data)
62
+ end
63
+ }
64
+ end
65
+
66
+ measure 1000, "Render only" do |times, file, template_class|
67
+ template = template_class.new(file)
68
+ puts Benchmark.measure {
69
+ scope = Scope.new
70
+ times.times { template.render(scope, data) }
71
+ }
72
+ end
@@ -0,0 +1,60 @@
1
+ <div>
2
+ <p><%= say_hello %></p>
3
+ <p><%= foo('bar') %></p>
4
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
5
+ <p><%= say_hello %></p>
6
+ <% if development %>
7
+ <p><%= "Dev rocks!" %></p>
8
+ <% end %>
9
+ <p><%= fruity_song %></p>
10
+ <div>
11
+ <p><%= say_hello %></p>
12
+ <p><%= foo('bar') %></p>
13
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
14
+ <p><%= say_hello %></p>
15
+ <% if development %>
16
+ <p><%= "Dev rocks!" %></p>
17
+ <% end %>
18
+ <p><%= fruity_song %></p>
19
+ <div>
20
+ <p><%= say_hello %></p>
21
+ <p><%= foo('bar') %></p>
22
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
23
+ <p><%= say_hello %></p>
24
+ <% if development %>
25
+ <p><%= "Dev rocks!" %></p>
26
+ <% end %>
27
+ <p><%= fruity_song %></p>
28
+ <div>
29
+ <p><%= say_hello %></p>
30
+ <p><%= foo('bar') %></p>
31
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
32
+ <p><%= say_hello %></p>
33
+ <% if development %>
34
+ <p><%= "Dev rocks!" %></p>
35
+ <% end %>
36
+ <p><%= fruity_song %></p>
37
+ <div>
38
+ <p><%= say_hello %></p>
39
+ <p><%= foo('bar') %></p>
40
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
41
+ <p><%= say_hello %></p>
42
+ <% if development %>
43
+ <p><%= "Dev rocks!" %></p>
44
+ <% end %>
45
+ <p><%= fruity_song %></p>
46
+ <div>
47
+ <p><%= say_hello %></p>
48
+ <p><%= foo('bar') %></p>
49
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
50
+ <p><%= say_hello %></p>
51
+ <% if development %>
52
+ <p><%= "Dev rocks!" %></p>
53
+ <% end %>
54
+ <p><%= fruity_song %></p>
55
+ </div>
56
+ </div>
57
+ </div>
58
+ </div>
59
+ </div>
60
+ </div>
@@ -0,0 +1,48 @@
1
+ %div
2
+ %p= say_hello
3
+ %p= foo('bar')
4
+ %p= play("The Beatles", 'I Am the Walrus')
5
+ - if development
6
+ %p= "Dev rocks!"
7
+
8
+ %p= fruity_song
9
+ %div
10
+ %p= say_hello
11
+ %p= foo('bar')
12
+ %p= play("The Beatles", 'I Am the Walrus')
13
+ - if development
14
+ %p= "Dev rocks!"
15
+
16
+ %p= fruity_song
17
+ %div
18
+ %p= say_hello
19
+ %p= foo('bar')
20
+ %p= play("The Beatles", 'I Am the Walrus')
21
+ - if development
22
+ %p= "Dev rocks!"
23
+
24
+ %p= fruity_song
25
+ %div
26
+ %p= say_hello
27
+ %p= foo('bar')
28
+ %p= play("The Beatles", 'I Am the Walrus')
29
+ - if development
30
+ %p= "Dev rocks!"
31
+
32
+ %p= fruity_song
33
+ %div
34
+ %p= say_hello
35
+ %p= foo('bar')
36
+ %p= play("The Beatles", 'I Am the Walrus')
37
+ - if development
38
+ %p= "Dev rocks!"
39
+
40
+ %p= fruity_song
41
+ %div
42
+ %p= say_hello
43
+ %p= foo('bar')
44
+ %p= play("The Beatles", 'I Am the Walrus')
45
+ - if development
46
+ %p= "Dev rocks!"
47
+
48
+ %p= fruity_song
@@ -0,0 +1,48 @@
1
+ div
2
+ p= say_hello
3
+ p= foo('bar')
4
+ p= play("The Beatles", 'I Am the Walrus')
5
+ if development
6
+ p= "Dev rocks!"
7
+
8
+ p= fruity_song
9
+ div
10
+ p= say_hello
11
+ p= foo('bar')
12
+ p= play("The Beatles", 'I Am the Walrus')
13
+ if development
14
+ p= "Dev rocks!"
15
+
16
+ p= fruity_song
17
+ div
18
+ p= say_hello
19
+ p= foo('bar')
20
+ p= play("The Beatles", 'I Am the Walrus')
21
+ if development
22
+ p= "Dev rocks!"
23
+
24
+ p= fruity_song
25
+ div
26
+ p= say_hello
27
+ p= foo('bar')
28
+ p= play("The Beatles", 'I Am the Walrus')
29
+ if development
30
+ p= "Dev rocks!"
31
+
32
+ p= fruity_song
33
+ div
34
+ p= say_hello
35
+ p= foo('bar')
36
+ p= play("The Beatles", 'I Am the Walrus')
37
+ if development
38
+ p= "Dev rocks!"
39
+
40
+ p= fruity_song
41
+ div
42
+ p= say_hello
43
+ p= foo('bar')
44
+ p= play("The Beatles", 'I Am the Walrus')
45
+ if development
46
+ p= "Dev rocks!"
47
+
48
+ p= fruity_song
@@ -0,0 +1,3 @@
1
+ div
2
+ | I like
3
+ p= fruity_song
@@ -0,0 +1,10 @@
1
+ <div>
2
+ <p><%= say_hello %></p>
3
+ <p><%= foo('bar') %></p>
4
+ <p><%= play("The Beatles", 'I Am the Walrus') %></p>
5
+ <p><%= say_hello %></p>
6
+ <% if development %>
7
+ <p><%= "Dev rocks!" %></p>
8
+ <% end %>
9
+ <p><%= fruity_song %></p>
10
+ </div>
@@ -0,0 +1,8 @@
1
+ %div
2
+ %p= say_hello
3
+ %p= foo('bar')
4
+ %p= play("The Beatles", 'I Am the Walrus')
5
+ - if development
6
+ %p= "Dev rocks!"
7
+
8
+ %p= fruity_song
@@ -0,0 +1,8 @@
1
+ div
2
+ p= say_hello
3
+ p= foo('bar')
4
+ p= play("The Beatles", 'I Am the Walrus')
5
+ if development
6
+ p= "Dev rocks!"
7
+
8
+ p= fruity_song
@@ -1,5 +1,5 @@
1
1
  module Tilt
2
2
  module Jadeite
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/lib/tilt-jadeite.rb CHANGED
@@ -6,6 +6,9 @@ require 'tilt'
6
6
 
7
7
  module Tilt
8
8
  module Jadeite
9
+
10
+ class << self; attr_accessor :environment end
11
+
9
12
  class JadeTemplate < Template
10
13
  self.default_mime_type = "text/html"
11
14
 
@@ -14,13 +17,16 @@ module Tilt
14
17
  require_template_library 'jade'
15
18
  end
16
19
 
20
+ def environment
21
+ Jadeite.environment ||= ::Jadeite::Environment.new
22
+ end
23
+
17
24
  def prepare
18
- env = ::Jadeite::Environment.new
19
- @compiled = env.compile(data, :filename => eval_file)
25
+ @compiled ||= environment.compile(data, :filename => eval_file)
20
26
  end
21
27
 
22
28
  def evaluate(scope, locals, &block)
23
- @compiled.render(locals.merge(scope.is_a?(Hash) ? scope : {}))
29
+ @compiled.render(locals)
24
30
  end
25
31
  end
26
32
  end
@@ -0,0 +1,6 @@
1
+ <div>
2
+ <p>Hello World!</p>
3
+ <p>Foo says bar</p>
4
+ <p>Play I Am the Walrus from The Beatles</p>
5
+ <p>Dev rocks!</p>
6
+ </div>
@@ -0,0 +1,6 @@
1
+ div
2
+ p= say_hello()
3
+ p= foo('bar')
4
+ p= play("The Beatles", 'I Am the Walrus')
5
+ if development
6
+ p= "Dev rocks!"
data/spec/sinatra_spec.rb CHANGED
@@ -10,6 +10,24 @@ class JadeApp < Sinatra::Base
10
10
 
11
11
  helpers Sinatra::Jade
12
12
 
13
+ helpers do
14
+ def foo(what)
15
+ "Foo says #{what}"
16
+ end
17
+
18
+ def play(artist, song)
19
+ "Play #{song} from #{artist}"
20
+ end
21
+
22
+ def say_hello
23
+ "Hello World!"
24
+ end
25
+
26
+ def development
27
+ true
28
+ end
29
+ end
30
+
13
31
  get "/string" do
14
32
  jade 'p this is soo #{adjective.substring(0,5) + "in awesome"}', :locals => {:adjective => "freaky"}
15
33
  end
@@ -26,6 +44,15 @@ class JadeApp < Sinatra::Base
26
44
  jade "include fixtures/views/includes/head"
27
45
  end
28
46
 
47
+ get "/helper_lambdas" do
48
+ jade :helper_lambdas, :locals => {
49
+ :foo => proc {|_,what| foo(what) },
50
+ :play => proc {|_,artist, song| play(artist, song) },
51
+ :say_hello => ->{ say_hello },
52
+ :development => ->{ development? }
53
+ }
54
+ end
55
+
29
56
  end
30
57
 
31
58
  describe "Sinatra helper" do
@@ -63,4 +90,11 @@ describe "Sinatra helper" do
63
90
  end
64
91
  end
65
92
 
93
+ it "calls helper methods on scope" do
94
+ get "/helper_lambdas"
95
+ #last_response.status.should eq 200
96
+ verify :format => :html do
97
+ last_response.body
98
+ end
99
+ end
66
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tilt-jadeite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000 Z
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -122,13 +122,23 @@ files:
122
122
  - LICENSE
123
123
  - README.md
124
124
  - Rakefile
125
+ - benchmarks/benchmarks.rb
126
+ - benchmarks/large.erb
127
+ - benchmarks/large.haml
128
+ - benchmarks/large.jade
129
+ - benchmarks/simple.jade
130
+ - benchmarks/template.erb
131
+ - benchmarks/template.haml
132
+ - benchmarks/template.jade
125
133
  - lib/tilt-jadeite.rb
126
134
  - lib/tilt-jadeite/sinatra.rb
127
135
  - lib/tilt-jadeite/version.rb
136
+ - spec/fixtures/approvals/sinatra_helper_calls_helper_methods_on_scope.approved.html
128
137
  - spec/fixtures/approvals/sinatra_helper_handles_includes_from_a_jade_template_in_the_sinatra_route.approved.html
129
138
  - spec/fixtures/approvals/sinatra_helper_handles_includes_of_other_jade_files.approved.html
130
139
  - spec/fixtures/approvals/sinatra_helper_renders_a_simple_jade_template_passing_a_couple_of_variables.approved.html
131
140
  - spec/fixtures/approvals/sinatra_helper_renders_a_string.approved.html
141
+ - spec/fixtures/views/helper_lambdas.jade
132
142
  - spec/fixtures/views/include.jade
133
143
  - spec/fixtures/views/includes/foot.jade
134
144
  - spec/fixtures/views/includes/head.jade
@@ -161,10 +171,12 @@ signing_key:
161
171
  specification_version: 3
162
172
  summary: Also adds Jade support to Sinatra
163
173
  test_files:
174
+ - spec/fixtures/approvals/sinatra_helper_calls_helper_methods_on_scope.approved.html
164
175
  - spec/fixtures/approvals/sinatra_helper_handles_includes_from_a_jade_template_in_the_sinatra_route.approved.html
165
176
  - spec/fixtures/approvals/sinatra_helper_handles_includes_of_other_jade_files.approved.html
166
177
  - spec/fixtures/approvals/sinatra_helper_renders_a_simple_jade_template_passing_a_couple_of_variables.approved.html
167
178
  - spec/fixtures/approvals/sinatra_helper_renders_a_string.approved.html
179
+ - spec/fixtures/views/helper_lambdas.jade
168
180
  - spec/fixtures/views/include.jade
169
181
  - spec/fixtures/views/includes/foot.jade
170
182
  - spec/fixtures/views/includes/head.jade