Skip to content

Instantly share code, notes, and snippets.

@todesking
Created February 27, 2014 17:20
Show Gist options
  • Save todesking/9254681 to your computer and use it in GitHub Desktop.
Save todesking/9254681 to your computer and use it in GitHub Desktop.
Write Rails helper method with any template engine
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