Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

assign ivars from view instead of doing a complicated runtime lookup …

…and matching
  • Loading branch information...
commit af60cd91c6c8ed78d7bc86b0241070f9683dc8d6 1 parent e27c36f
@svenfuchs authored
View
2  README.textile
@@ -1,4 +1,4 @@
-Minimal::Template is an experimental, minimalistic templating engine inspired by "Markaby":http://github.com/markaby/markaby & "Erector":http://erector.rubyforge.org but much smaller (~60 loc) and targeted at Rails 3.
+Minimal::Template is an experimental, minimalistic templating engine inspired by "Markaby":http://github.com/markaby/markaby & "Erector":http://erector.rubyforge.org but much smaller (~55 loc) and targeted at Rails 3.
<pre># views/foo/bar.rb
module Foo
View
11 lib/minimal/template.rb
@@ -6,13 +6,13 @@ class Minimal::Template
AUTO_BUFFER = %r(render|tag|error_message_|select|debug|_to|[^l]_for)
TAG_NAMES = %w(abbr a body div em fieldset h1 h2 h3 h4 h5 h6 head html img input label li
link ol option p pre script select span strong table thead tbody tfoot td title th tr ul)
- SKIP_IVARS = [:controller]
module Base
attr_accessor :view, :locals, :block
def initialize(view = nil)
@view, @locals, @_auto_buffer = view, {}, {}
+ view.assigns.each { |name, value| instance_variable_set(:"@#{name}", value) } if view
end
def _render(locals = nil, format = :html, &block)
@@ -24,12 +24,10 @@ def _render(locals = nil, format = :html, &block)
TAG_NAMES.each do |name|
define_method(name) { |*args, &block| content_tag(name, *args, &block) }
- # define_method("#{name}_for") { |*args, &block| content_tag_for(name, *args, &block) }
end
def <<(output)
- view.output_buffer << output
- view.output_buffer << "\n".html_safe
+ view.output_buffer << output << "\n".html_safe
end
alias :output :<<
@@ -41,15 +39,10 @@ def respond_to?(method)
def method_missing(method, *args, &block)
return locals[method] if locals.key?(method)
- return view.instance_variable_get("@#{method}") if ivar?(method)
return call_view(method, *args, &block) if view.respond_to?(method)
super
end
- def ivar?(method)
- !SKIP_IVARS.include?(method) && view.instance_variable_defined?("@#{method}") rescue false
- end
-
def call_view(method, *args, &block)
view.send(method, *args, &block).tap { |result| self << result if auto_buffer?(method) }
end
View
7 test/fixtures/views/foo/ivar.rb
@@ -0,0 +1,7 @@
+module Foo
+ class Ivar < Minimal::Template
+ def to_html
+ p { @foo }
+ end
+ end
+end
View
5 test/template_test.rb
@@ -73,6 +73,11 @@ def template
assert_equal html, view.render(:file => 'foo/simple', :locals => { :local => 'local' }).gsub("\n", '')
end
+ test "accessing an assigned instance_variable" do
+ view.assign(:foo => 'foo')
+ assert_equal '<p>foo</p>', view.render(:file => 'foo/ivar').gsub("\n", '')
+ end
+
test "partial rendering" do
html = '<div><p>foo</p><p>bar</p></div><p>baz</p>'
assert_equal html, view.render(:file => 'foo/partial').gsub("\n", '')
Please sign in to comment.
Something went wrong with that request. Please try again.