Sha256: 3c45f63e58bb46a787e17ee43dfc944109f3e69101657ce5d19cbcd0abc3353a

Contents?: true

Size: 1 KB

Versions: 24

Compression:

Stored size: 1 KB

Contents

require 'tilt'

module Luca
  class Template < Tilt::Template
    attr_accessor :namespace
    def self.default_mime_type
      'application/javascript'
    end

    def self.engine_initialized?
      defined? ::EJS
      defined? ::Haml
    end

    def initialize_engine
      require_template_library 'ejs'
      require_template_library 'haml'
    end

    def prepare
      options = @options.merge(:filename => eval_file, :line => line, :escape_attrs => false)
      @engine = ::Haml::Engine.new(data, options)
      self.namespace = "window.JST"
    end

    def evaluate(scope, locals, &block)
      compiled = @engine.render(scope, locals, &block)
      code = EJS.compile(compiled)
      tmpl = scope.logical_path 

      tmpl.gsub! /^.*\/templates\//, ''

      <<-JST

(function() {
  #{namespace} || (#{namespace} = {});
  #{namespace}[#{ tmpl.inspect }] = #{indent(code)};
}).call(this);

      JST
    end

    private

      def indent(string)
        string.gsub(/$(.)/m, "\\1  ").strip
      end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
luca-0.9.2 lib/luca/template.rb
luca-0.9.1 lib/luca/template.rb
luca-0.9.0 lib/luca/template.rb
luca-0.8.599 lib/luca/template.rb
luca-0.8.59 lib/luca/template.rb
luca-0.8.3 lib/luca/template.rb
luca-0.8.2 lib/luca/template.rb
luca-0.8.06 lib/luca/template.rb
luca-0.8 lib/luca/template.rb
luca-0.7.92 lib/luca/template.rb
luca-0.7.91 lib/luca/template.rb
luca-0.7.9 lib/luca/template.rb
luca-0.7.8 lib/luca/template.rb
luca-0.7.7 lib/luca/template.rb
luca-0.7.6 lib/luca/template.rb
luca-0.7.5 lib/luca/template.rb
luca-0.7.4 lib/luca/template.rb
luca-0.7.3 lib/luca/template.rb
luca-0.7.2 lib/luca/template.rb
luca-0.7.0 lib/luca/template.rb