Projects/Ferb
Ferb: Functions+ERb
I like Rails a lot, but like most programmers I have my particular tastes so parts of Rails (and Merb, for that matter) just don’t sit right with me, one of those parts is rendering and partials.
Rendering partials is particularly ugly just look:
render :partial => "account", :locals => { :account => @buyer }
Why not a simple function call like:
render_account(@buyer)
or, if you would like a hash:
render_account(:account => @buyer)
To make things nice we have ferb. Just install in the usual way:
$ gem install ferb
and you’re off.
Here’s an example of use:
require 'ferb'
class Foo
include Ferb
def_template :woo, :file => 'sample1.erb'
end
Where sample1.erb looks like:
<!-- |a,b = {}, *c, &d| -->
<html>
<body>
a:<%= a.inspect %><br/>
b:<%= b.inspect %><br/>
c:<%= c.inspect %><br/>
d:<%= d.inspect %><br/>
</body>
</html>
The first line comment embeds the argument signature, all standard argument types are supported. You can now call the function woo normally and the template file will be used (don’t worry everything is compiled once).
g = Foo.new
g.woo('A',{:b => 2},1,2) { "ggg" }
<html>
<body>
a:"A"<br/>
b:{:b=>2}<br/>
c:[1, 2]<br/>
d:#<Proc:0xb7c07b34@(irb):13><br/>
</body>
</html>
There is a helper module for use with Rails, simply add
require('ferb_helper')
in your config/environment.rb file, and
require 'ferb'
Ferb.enable_reload=true
in your config/environments/development.rb file (this setting makes ferb reload template files if updated during development).
Finally, in any helper file you can simply include
module MyHelper
include FerbHelper
end
and get the def_template goodness. To make ferb magic available in controllers add this to your application controller:
class ApplicationController < ActionController::Base
def self.inherited(subclass)
super(subclass)
subclass.send :include, FerbHelper
end
end
Now you can banish rendering contortions and partial madness.
Enjoy.

