Created
February 27, 2014 17:20
-
-
Save todesking/9254681 to your computer and use it in GitHub Desktop.
Write Rails helper method with any template engine
This file contains 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
module ApplicationHelper | |
def self.define_with_template(helper_name, template_type, template, locals = []) | |
compiled = Tilt[template_type].new(nil) { template } | |
define_method(helper_name) do|*args, &block| | |
compiled.render(self, Hash[*locals.zip(args).flatten], &block).html_safe | |
end | |
end | |
define_with_template :render_user_name, :slim, <<-SLIM, [:user_name] | |
.user | |
marquee | |
strong = user_name | |
SLIM | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment