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
'Hello World'
end
end
Foo.new.render # <h1>Hello World</h1>
__END__
Alternatively, an instance of EndView
can be extended:
module Baz
extend EndView.new(__FILE__)
def self.my_method
'Howdy World'
end
end
Baz.render # <h3>Howdy World</h3>
__END__
Template Engine
By default Tilt's Haml template engine is used, but alternative engines can be passed in:
require 'tilt/erb'
class Ham
include EndView.new(__FILE__, Tilt::ERBTemplate)
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 be 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
end
Fizz.new.render # <html><h1>Goodbye</h1></html>
__END__
For layouts that need to be dynamically initialized, self.layout
can be passed a lambda:
class MyDynamicLayout
include EndView.new(__FILE__)
def initialize(title)
@title = title
end
end
__END__
class Whizz
include EndView.new(__FILE__)
self.layout = -> { MyDynamicLayout.new('Hallo') }
end
Whizz.new.render # <html> <h1>Hallo</h1> <p>Bonjour</p> </html>
__END__
Inheritance
Templates are inherited from parent view models:
class Bar < Foo
def my_method
'Porridge'
end
end
Bar.new.render # <h1>Porridge</h1>
To override an inherited template, call the compile
class method:
class Pop < Foo
compile(__FILE__)
end
Pop.new.render # <p class="inherited">Hello World</p>
__END__
locals
Locals can be passed into the render method that override the view models:
Foo.new.render(my_method: 'Stranger') # <h1>Stranger</h1>
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