Skip to content

Commit b1d028b

Browse files
committed
Added ActionView template engine.
1 parent f71d058 commit b1d028b

File tree

7 files changed

+32
-2
lines changed

7 files changed

+32
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ end
2626

2727
Plain strings (i.e. not helpers output or variable values) should be escaped by `t` or `h` helper before passing them to `~` for output. `t` should be used for text and `h` should be used for HTML.
2828

29+
Inline Templates can also be used in views. It handles `.rit` files with same syntax.
30+
2931
## Installation
3032

3133
Add this line to your application's Gemfile:

lib/inline_templates.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
require "inline_templates/blank_object"
77
require "inline_templates/buffer_wrapper"
88
require "inline_templates/rendering_context"
9-
require "inline_templates/helper"
9+
require "inline_templates/helpers"
10+
require "inline_templates/template_handler.rb"
1011

1112
module InlineTemplates
1213
def self.render(view, details, locals, &block)
@@ -31,7 +32,8 @@ def self.render(view, details, locals, &block)
3132
end
3233

3334
template = ActionView::Template.new("", identifier, handler, details)
34-
template.send :compile!, view
3535
template.render view, locals
3636
end
3737
end
38+
39+
ActionView::Template.register_template_handler :rit, InlineTemplates::TemplateHandler.new
File renamed without changes.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module InlineTemplates
2+
class TemplateHandler
3+
def call(source)
4+
<<-EOF
5+
@output_buffer ||= ActionView::OutputBuffer.new
6+
context = ::InlineTemplates::RenderingContext.new(self, local_assigns, ::InlineTemplates::Builder.new)
7+
context.instance_exec do
8+
#{source.source}
9+
end
10+
@output_buffer.to_s
11+
EOF
12+
end
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'spec_helper'
2+
3+
describe InlineTemplates::TemplateHandler do
4+
it "renders" do
5+
invoke_controller(TestController, :test_file).should == TestController::ReferenceOutput
6+
end
7+
end

spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ def test
3131
~ div("test")
3232
end
3333
end
34+
35+
def test_file
36+
render :file => File.expand_path("../support/test.rit", __FILE__)
37+
end
3438
end
3539

spec/support/test.rit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
~ div("test")

0 commit comments

Comments
 (0)