Sha256: a66d1c0ec2e0b0c19d17034189854e4712d1d4fa6b68d2ff7a42697405c67522

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require "tilt/template"
require "execjs"
require 'sprockets'

module Templebars
  module Rails
    # Tilt template renderer for precompiling Handlebars templates and storing
    # them in a global Templates object.
    class HandlebarsTemplate < ::Tilt::Template
      def self.default_mime_type
        "application/javascript"
      end

      def prepare; end

      def evaluate( scope, locals, &block )
        name = scope.logical_path.sub( /^templates\//, "" )
        register_template_js( name, precompile( data ) )
      end

      protected

      def register_template_js( name, precompiled_js )
        global = Rails.application.config.templebars_template_global || "Templates"
        <<-JS
          this.#{global} || (this.#{global} = {});
          this.#{global}["#{name}"] = Handlebars.template(#{precompiled_js});
        JS
      end

      def precompile( template )
        runtime.call( "Handlebars.precompile", template, { data: {} } )
      end

      def runtime
        @runtime ||= ExecJS.compile( handlebars_js )
      end

      def handlebars_js
        path = File.expand_path( "../../../../vendor/assets/javascripts/handlebars.js", __FILE__ )
        File.read( path )
      end
    end

    Sprockets.register_engine '.handlebars', HandlebarsTemplate
    Sprockets.register_engine '.hbs', HandlebarsTemplate
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
templebars-0.0.9 lib/templebars/rails/templates.rb