Sha256: f64336021acaf1c608f0aeb247045f597e7718f7667e39e08fe4a32fa3bad336

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

require 'tilt'

module Luca
  class Template < Tilt::Template

    def self.namespace
      'JST'
    end

    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)
    end

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

      namespace = self.class.namespace 

      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

6 entries across 6 versions & 1 rubygems

Version Path
luca-0.9.8 lib/luca/template.rb
luca-0.9.76 lib/luca/template.rb
luca-0.9.7 lib/luca/template.rb
luca-0.9.65 lib/luca/template.rb
luca-0.9.6 lib/luca/template.rb
luca-0.9.42 lib/luca/template.rb