Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…template types
  • Loading branch information...
commit 37a3185ff926b50578e597b43a17d69ab2697acf 1 parent 58b5485
@trans trans authored
Showing with 30 additions and 1 deletion.
  1. +12 −1 lib/tilt.rb
  2. +18 −0 test/tilt_htmltemplate_test.rb
View
13 lib/tilt.rb
@@ -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
View
18 test/tilt_htmltemplate_test.rb
@@ -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

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
Owner

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

@trans

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

A thought on implementation. So

   Tilt[ext]

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

@rtomayko
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.

@trans

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

Please sign in to comment.
Something went wrong with that request. Please try again.