Permalink
Browse files

introduce HTMLTemplate allowing 'polymorphic' use of HTML with other …

…template types
  • Loading branch information...
trans committed Jan 29, 2010
1 parent 58b5485 commit 37a3185ff926b50578e597b43a17d69ab2697acf
Showing with 30 additions and 1 deletion.
  1. +12 −1 lib/tilt.rb
  2. +18 −0 test/tilt_htmltemplate_test.rb
View
@@ -92,7 +92,6 @@ def initialize_engine
@engine_initialized = false
class << self ; attr_accessor :engine_initialized ; end
-
# Load template source and compile the template. The template is
# loaded and compiled the first time this method is called; subsequent
# calls are no-ops.
@@ -206,6 +205,18 @@ def template_source
register 'str', StringTemplate
+ # HTML pass-thru template.
+ class HTMLTemplate < Template
+ def compile!
+ end
+
+ def evaluate(scope, locals, &block)
+ data
+ end
+ end
+ register 'html', HTMLTemplate
+
+
# ERB template implementation. See:
# http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
class ERBTemplate < Template
@@ -0,0 +1,18 @@
+require 'contest'
+require 'tilt'
+
+begin
+ class HTMLTemplateTest < Test::Unit::TestCase
+ test "is registered for '.html' files" do
+ assert_equal Tilt::HTMLTemplate, Tilt['test.html']
+ end
+
+ test "compiling and evaluating the template with #render" do
+ template = Tilt::HTMLTemplate.new { |t| "<html><head></head><body>Hello World!</body></html>" }
+ assert_equal "<html><head></head><body>Hello World!</body></html>", template.render
+ end
+ end
+rescue LoadError => boom
+ warn "Tilt::HTMLTemplate (disabled)\n"
+end
+

6 comments on commit 37a3185

@josh

This comment has been minimized.

Show comment Hide comment
@josh

josh Jan 29, 2010

Contributor

I'm not sure I like this approach.

I believe the intent of this is to make "foo.html" just pass through. Well what about "foo.xml", "foo.rss", etc. It seems like a broken model to register "formats" as "template handlers".

My proposal would be to have Tilt simply return the file as is if there are no registered handlers for the extension.

Contributor

josh replied Jan 29, 2010

I'm not sure I like this approach.

I believe the intent of this is to make "foo.html" just pass through. Well what about "foo.xml", "foo.rss", etc. It seems like a broken model to register "formats" as "template handlers".

My proposal would be to have Tilt simply return the file as is if there are no registered handlers for the extension.

@rtomayko

This comment has been minimized.

Show comment Hide comment
@rtomayko

rtomayko Jan 29, 2010

Owner

Yeah that's a really good point. This should be handled generically.

Owner

rtomayko replied Jan 29, 2010

Yeah that's a really good point. This should be handled generically.

@trans

This comment has been minimized.

Show comment Hide comment
@trans

trans Jan 29, 2010

Contributor

Ah, I didn't consider the possibility of other formats. I agree, passing thru unregistered extension is probably the way to go. Should be simple enough to implement too.

Contributor

trans replied Jan 29, 2010

Ah, I didn't consider the possibility of other formats. I agree, passing thru unregistered extension is probably the way to go. Should be simple enough to implement too.

@trans

This comment has been minimized.

Show comment Hide comment
@trans

trans Jan 29, 2010

Contributor

A thought on implementation. So

   Tilt[ext]

would return something like VerbatimTemplate if ext isn't registered.

Contributor

trans replied Jan 29, 2010

A thought on implementation. So

   Tilt[ext]

would return something like VerbatimTemplate if ext isn't registered.

@rtomayko

This comment has been minimized.

Show comment Hide comment
@rtomayko

rtomayko Jan 29, 2010

Owner

I like that. There should be a way to test for a non-match, though. This could be a simple predicate method on Template that returns false everywhere except for VerbatimTemplate.

Owner

rtomayko replied Jan 29, 2010

I like that. There should be a way to test for a non-match, though. This could be a simple predicate method on Template that returns false everywhere except for VerbatimTemplate.

@trans

This comment has been minimized.

Show comment Hide comment
@trans

trans Jan 29, 2010

Contributor

Sounds good. Plus something like Tilt.registered?(ext).

Contributor

trans replied Jan 29, 2010

Sounds good. Plus something like Tilt.registered?(ext).

Please sign in to comment.