Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created April 20, 2011 14:31
Show Gist options
  • Save judofyr/931483 to your computer and use it in GitHub Desktop.
Save judofyr/931483 to your computer and use it in GitHub Desktop.

Tilt's main features

All of these features work with each other.

# Helper method
def render(tmpl, scope = Object.new, locals = {}, &blk)
  Tilt['erb'].new { tmpl }.render(scope, locals, &blk)
end

Custom scope (without monkey patching of any kind)

render('<%= self %>', 123) # => 123
Foo = Struct.new(:foo)
render('<%= foo %>, Foo.new(456)) # => 456

Passing a block

# Doesn't work with Proc.new
render('<%= yield %>') { 123 } # => 123

Constant access

class Scope
  FOO = 123
end

# This can't be handled with a simple Proc.new
render('<%= FOO %>', Scope.new) # => 123

Also, when you pass BasicObject as scope, you'll still have access to constants defined on Object:

render('<%= String %>', BasicObject.new) # => "String"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment