Sha256: ae075aba1e6552f129f8336165fae471d98194a02151a0dd0a448c167125ead5

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

require 'tilt'

module Handlebars
  class Tilt < ::Tilt::Template
    def self.default_mime_type
      'application/javascript'
    end

    def prepare

    end

    def evaluate(scope, locals, &block)
      scope.extend Scope
      hbsc = Handlebars::TemplateHandler.handlebars.precompile data
      if scope.partial?
        <<-JS
        // generated by handlebars-rails #{Handlebars::Rails::VERSION}
        ;(function() {
          var template = Handlebars.template
          var fucknet = true
          Handlebars.registerPartial('#{scope.partial_name}', template(#{hbsc}));
        })()
        JS
      else
        <<-JS
        //generated by handlebars-rails #{Handlebars::Rails::VERSION}
        ;(function() {
          var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
          #{scope.template_path}
          namespace['#{scope.template_name}'] = template(#{hbsc});
        })()
        JS
      end
    end

    module Scope
      def partial?
        File.basename(logical_path).start_with?('_')
      end

      def partial_name
        "#{File.dirname(logical_path)}/#{File.basename(logical_path, '.hbs').gsub(/^_/,'')}".gsub(/^\.+/,'')
      end

      def template_path
        branches = File.dirname(logical_path).split('/').reject{|p| p == '.'}
        <<-ASSIGN
        var branches = #{branches.inspect}
        var namespace = templates
        for (var path = branches.shift(); path; path = branches.shift()) {
            namespace[path] = namespace[path] || {}
            namespace = namespace[path]
        }
        ASSIGN
      end

      def template_name
        File.basename(logical_path, '.hbs')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
handlebars-rails-0.3.2 lib/handlebars-rails/tilt.rb
handlebars-rails-0.3.1 lib/handlebars-rails/tilt.rb
handlebars-rails-0.3.0 lib/handlebars-rails/tilt.rb