Created
March 7, 2019 21:56
-
-
Save matsadler/e45bcbf928cdc4e4afa901268c9b1bd6 to your computer and use it in GitHub Desktop.
Hack to enable refinements in Sinatra (Tilt) templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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