tilt 0.9 → 0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -0
- data/TEMPLATES.md +53 -0
- data/lib/tilt.rb +51 -4
- data/test/tilt_erbtemplate_test.rb +17 -0
- data/test/tilt_erubistemplate_test.rb +17 -0
- data/test/tilt_mustache_views/external.rb +10 -5
- data/test/tilt_radiustemplate_test.rb +66 -0
- data/test/tilt_sasstemplate_test.rb +2 -2
- data/test/tilt_template_test.rb +16 -0
- data/tilt.gemspec +7 -2
- metadata +20 -6
data/README.md
CHANGED
@@ -33,6 +33,8 @@ Support for these template engines is included with the package:
|
|
33
33
|
Mustache .mustache mustache
|
34
34
|
RDiscount .markdown rdiscount
|
35
35
|
RedCloth .textile redcloth
|
36
|
+
RDoc .rdoc rdoc
|
37
|
+
Radius .radius radius
|
36
38
|
|
37
39
|
See [TEMPLATES.md][t] for detailed information on template engine
|
38
40
|
options and supported features.
|
data/TEMPLATES.md
CHANGED
@@ -432,3 +432,56 @@ engine in a threaded environment.
|
|
432
432
|
### See also
|
433
433
|
|
434
434
|
* [RDoc](http://rdoc.sourceforge.net/doc/index.html)
|
435
|
+
|
436
|
+
|
437
|
+
<a name='radius'></a>
|
438
|
+
Radius (`radius`)
|
439
|
+
-----------------
|
440
|
+
|
441
|
+
Radius is the template language used by Radiant CMS. It is a tag
|
442
|
+
language designed to be valid XML/HTML.
|
443
|
+
|
444
|
+
### Example
|
445
|
+
|
446
|
+
<html>
|
447
|
+
<body>
|
448
|
+
<h1><r:title /></h1>
|
449
|
+
<ul class="<r:type />">
|
450
|
+
<r:repeat times="3">
|
451
|
+
<li><r:hello />!</li>
|
452
|
+
</r:repeat>
|
453
|
+
</ul>
|
454
|
+
<r:yield />
|
455
|
+
</body>
|
456
|
+
</html>
|
457
|
+
|
458
|
+
### Usage
|
459
|
+
|
460
|
+
To render a template such as the one above.
|
461
|
+
|
462
|
+
scope = OpenStruct.new
|
463
|
+
scope.title = "Radius Example"
|
464
|
+
scope.hello = "Hello, World!"
|
465
|
+
|
466
|
+
require 'radius'
|
467
|
+
template = Tilt::RadiusTemplate.new('example.radius', :tag_prefix=>'r')
|
468
|
+
template.render(scope, :type=>'hlist'){ "Jackpot!" }
|
469
|
+
|
470
|
+
The result will be:
|
471
|
+
|
472
|
+
<html>
|
473
|
+
<body>
|
474
|
+
<h1>Radius Example</h1>
|
475
|
+
<ul class="hlist">
|
476
|
+
<li>Hello, World!</li>
|
477
|
+
<li>Hello, World!</li>
|
478
|
+
<li>Hello, World!</li>
|
479
|
+
</ul>
|
480
|
+
Jackpot!
|
481
|
+
</body>
|
482
|
+
</html>
|
483
|
+
|
484
|
+
### See also
|
485
|
+
|
486
|
+
* [Radius](http://radius.rubyforge.org/)
|
487
|
+
* [Radiant](http://radiantcms.org/)
|
data/lib/tilt.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'digest/md5'
|
2
2
|
|
3
3
|
module Tilt
|
4
|
-
VERSION = '0.
|
4
|
+
VERSION = '0.10'
|
5
5
|
|
6
6
|
@template_mappings = {}
|
7
7
|
|
@@ -350,18 +350,29 @@ module Tilt
|
|
350
350
|
# ERB template implementation. See:
|
351
351
|
# http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
|
352
352
|
class ERBTemplate < Template
|
353
|
+
@@default_output_variable = '_erbout'
|
354
|
+
|
355
|
+
def self.default_output_variable
|
356
|
+
@@default_output_variable
|
357
|
+
end
|
358
|
+
|
359
|
+
def self.default_output_variable=(name)
|
360
|
+
@@default_output_variable = name
|
361
|
+
end
|
362
|
+
|
353
363
|
def initialize_engine
|
354
364
|
return if defined? ::ERB
|
355
365
|
require_template_library 'erb'
|
356
366
|
end
|
357
367
|
|
358
368
|
def prepare
|
359
|
-
@outvar =
|
369
|
+
@outvar = options[:outvar] || self.class.default_output_variable
|
360
370
|
@engine = ::ERB.new(data, options[:safe], options[:trim], @outvar)
|
361
371
|
end
|
362
372
|
|
363
373
|
def precompiled_template(locals)
|
364
|
-
@engine.src
|
374
|
+
source = @engine.src
|
375
|
+
source
|
365
376
|
end
|
366
377
|
|
367
378
|
def precompiled_preamble(locals)
|
@@ -414,7 +425,7 @@ module Tilt
|
|
414
425
|
|
415
426
|
def prepare
|
416
427
|
@options.merge!(:preamble => false, :postamble => false)
|
417
|
-
@outvar =
|
428
|
+
@outvar = options.delete(:outvar) || self.class.default_output_variable
|
418
429
|
engine_class = options.delete(:engine_class)
|
419
430
|
engine_class = ::Erubis::EscapedEruby if options.delete(:escape_html)
|
420
431
|
@engine = (engine_class || ::Erubis::Eruby).new(data, options)
|
@@ -752,4 +763,40 @@ module Tilt
|
|
752
763
|
end
|
753
764
|
end
|
754
765
|
register 'coffee', CoffeeTemplate
|
766
|
+
|
767
|
+
|
768
|
+
# Radius Template
|
769
|
+
# http://github.com/jlong/radius/
|
770
|
+
class RadiusTemplate < Template
|
771
|
+
def initialize_engine
|
772
|
+
return if defined? ::Radius
|
773
|
+
require_template_library 'radius'
|
774
|
+
end
|
775
|
+
|
776
|
+
def prepare
|
777
|
+
end
|
778
|
+
|
779
|
+
def evaluate(scope, locals, &block)
|
780
|
+
context = Class.new(Radius::Context).new
|
781
|
+
context.define_tag("yield") do
|
782
|
+
block.call
|
783
|
+
end
|
784
|
+
locals.each do |tag, value|
|
785
|
+
context.define_tag(tag) do
|
786
|
+
value
|
787
|
+
end
|
788
|
+
end
|
789
|
+
(class << context; self; end).class_eval do
|
790
|
+
define_method :tag_missing do |tag, attr, &block|
|
791
|
+
scope.__send__(tag) # any way to support attr as args?
|
792
|
+
end
|
793
|
+
end
|
794
|
+
options = {:tag_prefix => 'r'}.merge(@options)
|
795
|
+
parser = Radius::Parser.new(context, options)
|
796
|
+
parser.parse(data)
|
797
|
+
end
|
798
|
+
end
|
799
|
+
register 'radius', RadiusTemplate
|
800
|
+
|
755
801
|
end
|
802
|
+
|
@@ -29,6 +29,23 @@ class ERBTemplateTest < Test::Unit::TestCase
|
|
29
29
|
assert_equal "Hey Joe!", template.render(scope)
|
30
30
|
end
|
31
31
|
|
32
|
+
class MockOutputVariableScope
|
33
|
+
attr_accessor :exposed_buffer
|
34
|
+
end
|
35
|
+
|
36
|
+
test "exposing the buffer to the template by default" do
|
37
|
+
begin
|
38
|
+
Tilt::ERBTemplate.default_output_variable = '@_out_buf'
|
39
|
+
template = Tilt::ERBTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
|
40
|
+
scope = MockOutputVariableScope.new
|
41
|
+
template.render(scope)
|
42
|
+
assert_not_nil scope.exposed_buffer
|
43
|
+
assert_equal scope.exposed_buffer, 'hey'
|
44
|
+
ensure
|
45
|
+
Tilt::ERBTemplate.default_output_variable = '_erbout'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
32
49
|
test "passing a block for yield" do
|
33
50
|
template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
|
34
51
|
assert_equal "Hey Joe!", template.render { 'Joe' }
|
@@ -26,6 +26,23 @@ begin
|
|
26
26
|
assert_equal "Hey Joe!", template.render(scope)
|
27
27
|
end
|
28
28
|
|
29
|
+
class MockOutputVariableScope
|
30
|
+
attr_accessor :exposed_buffer
|
31
|
+
end
|
32
|
+
|
33
|
+
test "exposing the buffer to the template by default" do
|
34
|
+
begin
|
35
|
+
Tilt::ErubisTemplate.default_output_variable = '@_out_buf'
|
36
|
+
template = Tilt::ErubisTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
|
37
|
+
scope = MockOutputVariableScope.new
|
38
|
+
template.render(scope)
|
39
|
+
assert_not_nil scope.exposed_buffer
|
40
|
+
assert_equal scope.exposed_buffer, 'hey'
|
41
|
+
ensure
|
42
|
+
Tilt::ErubisTemplate.default_output_variable = '_erbout'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
29
46
|
test "passing a block for yield" do
|
30
47
|
template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' }
|
31
48
|
assert_equal "Hey Joe!", template.render { 'Joe' }
|
@@ -1,9 +1,14 @@
|
|
1
|
-
|
1
|
+
begin
|
2
|
+
require 'mustache'
|
2
3
|
|
3
|
-
module Views
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
module Views
|
5
|
+
class External < Mustache
|
6
|
+
def hello
|
7
|
+
"Stached"
|
8
|
+
end
|
7
9
|
end
|
8
10
|
end
|
11
|
+
|
12
|
+
rescue LoadError => boom
|
13
|
+
# silently fail, disabled message already displayed
|
9
14
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'contest'
|
2
|
+
require 'tilt'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'radius'
|
6
|
+
|
7
|
+
class RadiusTemplateTest < Test::Unit::TestCase
|
8
|
+
test "registered for '.radius' files" do
|
9
|
+
assert_equal Tilt::RadiusTemplate, Tilt['test.radius']
|
10
|
+
end
|
11
|
+
|
12
|
+
test "preparing and evaluating templates on #render" do
|
13
|
+
template = Tilt::RadiusTemplate.new { |t| "Hello World!" }
|
14
|
+
assert_equal "Hello World!", template.render
|
15
|
+
end
|
16
|
+
|
17
|
+
test "passing locals" do
|
18
|
+
template = Tilt::RadiusTemplate.new { "Hey <r:name />!" }
|
19
|
+
assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
|
20
|
+
end
|
21
|
+
|
22
|
+
class ExampleRadiusScope
|
23
|
+
def beer; 'wet'; end
|
24
|
+
def whisky; 'wetter'; end
|
25
|
+
end
|
26
|
+
|
27
|
+
test "combining scope and locals when scope responds" do
|
28
|
+
template = Tilt::RadiusTemplate.new {
|
29
|
+
'Beer is <r:beer /> but Whisky is <r:whisky />.'
|
30
|
+
}
|
31
|
+
scope = ExampleRadiusScope.new
|
32
|
+
assert_equal "Beer is wet but Whisky is wetter.", template.render(scope)
|
33
|
+
end
|
34
|
+
|
35
|
+
test "precedence when locals and scope define same variables" do
|
36
|
+
template = Tilt::RadiusTemplate.new {
|
37
|
+
'Beer is <r:beer /> but Whisky is <r:whisky />.'
|
38
|
+
}
|
39
|
+
scope = ExampleRadiusScope.new
|
40
|
+
assert_equal "Beer is great but Whisky is greater.",
|
41
|
+
template.render(scope, :beer => 'great', :whisky => 'greater')
|
42
|
+
end
|
43
|
+
|
44
|
+
#test "handles local scope" do
|
45
|
+
# beer = 'wet'
|
46
|
+
# whisky = 'wetter'
|
47
|
+
#
|
48
|
+
# template = Tilt::RadiusTemplate.new {
|
49
|
+
# 'Beer is <r:beer /> but Whisky is <r:whisky />.'
|
50
|
+
# }
|
51
|
+
# assert_equal "Beer is wet but Whisky is wetter.", template.render(self)
|
52
|
+
#end
|
53
|
+
|
54
|
+
test "passing a block for yield" do
|
55
|
+
template = Tilt::RadiusTemplate.new {
|
56
|
+
'Beer is <r:yield /> but Whisky is <r:yield />ter.'
|
57
|
+
}
|
58
|
+
assert_equal "Beer is wet but Whisky is wetter.",
|
59
|
+
template.render({}) { 'wet' }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
rescue LoadError => boom
|
64
|
+
warn "Tilt::RadiusTemplate (disabled)\n"
|
65
|
+
end
|
66
|
+
|
@@ -11,8 +11,8 @@ begin
|
|
11
11
|
end
|
12
12
|
|
13
13
|
test "compiles and evaluates the template on #render" do
|
14
|
-
template = Tilt::SassTemplate.new { |t| "#main\n :background-color #
|
15
|
-
assert_equal "#main {\n background-color: #
|
14
|
+
template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000f1" }
|
15
|
+
assert_equal "#main {\n background-color: #0000f1; }\n", template.render
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
data/test/tilt_template_test.rb
CHANGED
@@ -122,6 +122,8 @@ class TiltTemplateTest < Test::Unit::TestCase
|
|
122
122
|
end
|
123
123
|
|
124
124
|
class Person
|
125
|
+
CONSTANT = "Bob"
|
126
|
+
|
125
127
|
attr_accessor :name
|
126
128
|
def initialize(name)
|
127
129
|
@name = name
|
@@ -138,4 +140,18 @@ class TiltTemplateTest < Test::Unit::TestCase
|
|
138
140
|
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' }
|
139
141
|
assert_equal "Hey Joe!", inst.render(Object.new){ 'Joe' }
|
140
142
|
end
|
143
|
+
|
144
|
+
test "template which accesses a constant" do
|
145
|
+
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' }
|
146
|
+
assert_equal "Hey Bob!", inst.render(Person.new("Joe"))
|
147
|
+
end
|
148
|
+
|
149
|
+
class FastPerson < Person
|
150
|
+
include Tilt::CompileSite
|
151
|
+
end
|
152
|
+
|
153
|
+
test "template which accesses a constant with Tilt::CompileSite" do
|
154
|
+
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' }
|
155
|
+
assert_equal "Hey Bob!", inst.render(FastPerson.new("Joe"))
|
156
|
+
end
|
141
157
|
end
|
data/tilt.gemspec
CHANGED
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
4
4
|
|
5
5
|
s.name = 'tilt'
|
6
|
-
s.version = '0.
|
7
|
-
s.date = '2010-
|
6
|
+
s.version = '0.10'
|
7
|
+
s.date = '2010-05-27'
|
8
8
|
|
9
9
|
s.description = "Generic interface to multiple Ruby template engines"
|
10
10
|
s.summary = s.description
|
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
test/tilt_liquidtemplate_test.rb
|
32
32
|
test/tilt_mustache_views/external.rb
|
33
33
|
test/tilt_mustachetemplate_test.rb
|
34
|
+
test/tilt_radiustemplate_test.rb
|
34
35
|
test/tilt_rdiscounttemplate_test.rb
|
35
36
|
test/tilt_rdoctemplate_test.rb
|
36
37
|
test/tilt_redclothtemplate_test.rb
|
@@ -42,6 +43,9 @@ Gem::Specification.new do |s|
|
|
42
43
|
]
|
43
44
|
# = MANIFEST =
|
44
45
|
|
46
|
+
s.default_executable = 'tilt'
|
47
|
+
s.executables = ['tilt']
|
48
|
+
|
45
49
|
s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/}
|
46
50
|
s.add_development_dependency 'contest'
|
47
51
|
s.add_development_dependency 'builder'
|
@@ -52,6 +56,7 @@ Gem::Specification.new do |s|
|
|
52
56
|
s.add_development_dependency 'liquid'
|
53
57
|
s.add_development_dependency 'less'
|
54
58
|
s.add_development_dependency 'coffee-script'
|
59
|
+
s.add_development_dependency 'radius'
|
55
60
|
|
56
61
|
s.extra_rdoc_files = %w[COPYING]
|
57
62
|
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
version: "0.
|
7
|
+
- 10
|
8
|
+
version: "0.10"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Ryan Tomayko
|
@@ -13,8 +13,8 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date: 2010-
|
17
|
-
default_executable:
|
16
|
+
date: 2010-05-27 00:00:00 -07:00
|
17
|
+
default_executable: tilt
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: contest
|
@@ -126,10 +126,22 @@ dependencies:
|
|
126
126
|
version: "0"
|
127
127
|
type: :development
|
128
128
|
version_requirements: *id009
|
129
|
+
- !ruby/object:Gem::Dependency
|
130
|
+
name: radius
|
131
|
+
prerelease: false
|
132
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
type: :development
|
140
|
+
version_requirements: *id010
|
129
141
|
description: Generic interface to multiple Ruby template engines
|
130
142
|
email: r@tomayko.com
|
131
|
-
executables:
|
132
|
-
|
143
|
+
executables:
|
144
|
+
- tilt
|
133
145
|
extensions: []
|
134
146
|
|
135
147
|
extra_rdoc_files:
|
@@ -152,6 +164,7 @@ files:
|
|
152
164
|
- test/tilt_liquidtemplate_test.rb
|
153
165
|
- test/tilt_mustache_views/external.rb
|
154
166
|
- test/tilt_mustachetemplate_test.rb
|
167
|
+
- test/tilt_radiustemplate_test.rb
|
155
168
|
- test/tilt_rdiscounttemplate_test.rb
|
156
169
|
- test/tilt_rdoctemplate_test.rb
|
157
170
|
- test/tilt_redclothtemplate_test.rb
|
@@ -206,6 +219,7 @@ test_files:
|
|
206
219
|
- test/tilt_lesstemplate_test.rb
|
207
220
|
- test/tilt_liquidtemplate_test.rb
|
208
221
|
- test/tilt_mustachetemplate_test.rb
|
222
|
+
- test/tilt_radiustemplate_test.rb
|
209
223
|
- test/tilt_rdiscounttemplate_test.rb
|
210
224
|
- test/tilt_rdoctemplate_test.rb
|
211
225
|
- test/tilt_redclothtemplate_test.rb
|