lib/sht_rails/handlebars.rb in sht_rails-0.1.1 vs lib/sht_rails/handlebars.rb in sht_rails-0.2.0

- old
+ new

@@ -2,16 +2,29 @@ require "active_support" module ShtRails module Handlebars + def self.context(partials = nil) + @context = nil unless ActionView::Resolver.caching? + @context ||= begin + context = ::Handlebars::Context.new + if helpers = Rails.application.assets.find_asset(ShtRails.helper_path) + context.runtime.eval helpers.source + end + partials.each { |key, value| context.register_partial(key, value) } if partials + context + end + end + def self.call(template) if template.locals.include?(ShtRails.action_view_key.to_s) || template.locals.include?(ShtRails.action_view_key.to_sym) <<-SHT - hbs_context_for_sht = Handlebars::Context.new - partials.each do |key, value| - hbs_context_for_sht.register_partial(key, value) - end if defined?(partials) && partials.is_a?(Hash) + hbs_context_for_sht = if defined?(partials) && partials.is_a?(Hash) + ShtRails::Handlebars.context(partials) + else + ShtRails::Handlebars.context + end hbs_context_for_sht.compile(#{template.source.inspect}).call(#{ShtRails.action_view_key.to_s} || {}).html_safe SHT else "#{template.source.inspect}.html_safe" end \ No newline at end of file