Sha256: beffdd74475f3976f5fb61b381fdda188ae5fbaae1ad21a6adc57cc94d9b6773
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 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.1.0 | lib/templebars/rails/templates.rb |