EndView
View model and template in same file using the tilt gem.
Usage
To use, include a new instance of EndView, passing __File__ into the constructor and put the template at the bottom of the file after __END__.
require 'end_view'
class Foo
include EndView.new(__FILE__)
def my_method
'World'
end
end
Foo.new.render # Hello World
__END__
Alternatively, an instance of EndView can be extended:
module Baz
extend EndView.new(__FILE__)
def self.my_method
'World'
end
end
Baz.render # Howdy World
__END__
Template Engine
By default Tilt's ERB template engine is used, but alternative engines can be passed in:
require 'tilt/haml'
class Ham
include EndView.new(__FILE__, Tilt::HamlTemplate)
def my_method
'Heya'
end
end
Ham.new.render # <h1>Heya</h1>
__END__
To change the default engine:
EndView.default_engine = Tilt::LiquidTemplate
Layouts
For template engines that support it, view models can passed blocks:
module MyLayout
extend EndView.new(__FILE__)
end
MyLayout.render { "S'up" } # <html>S'up</html>
__END__
These can then be used as layouts:
class Fizz
include EndView.new(__FILE__)
self.layout = MyLayout
def my_method
'World'
end
end
Fizz.new.render # <html>Goodbye World</html>
__END__
Inheritance
Templates are inherited:
class Bar < Foo
def my_method
'Porridge'
end
end
Bar.new.render # Hello Porridge
To override an inherited template, call the compile class method:
class Pop < Foo
compile(__FILE__)
end
Pop.new.render # The World is too big!
__END__
locals
Locals can be passed into the render method that override the view models:
Foo.new.render(my_method: 'Stranger') # Hello Stranger
Installation
Add your Gemfile:
gem 'end_view'
Contributing
- Fork it ( https://github.com/[my-github-username]/end_view/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request