Skip to content

Instantly share code, notes, and snippets.

@matsadler
Created March 7, 2019 21:56
Show Gist options
  • Save matsadler/e45bcbf928cdc4e4afa901268c9b1bd6 to your computer and use it in GitHub Desktop.
Save matsadler/e45bcbf928cdc4e4afa901268c9b1bd6 to your computer and use it in GitHub Desktop.
Hack to enable refinements in Sinatra (Tilt) templates
require "sinatra/base"
class App < Sinatra::Base
using SomeRefinementModule
# hack to enable refinements in templates
module CompiledTemplates
_binding = binding # get the current binding
# add a class_eval method to the binding that does an eval in the binding
def _binding.class_eval(*args)
eval(*args)
end
# insert our binding into Tilt::Template as Object, so when it calls
# Object.class_eval it's actually calling _binding.eval
Tilt::Template::Object = _binding
# Tilt expects compiled methods in Tilt::TOPOBJECT, which also needs to
# be in scope for the _binding.eval, set those to this module
Tilt::TOPOBJECT = TOPOBJECT = self
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment