Sha256: 52516b88996e956176c94b5352a2918846d644d35203c820b759c3235b621dc2

Contents?: true

Size: 1.2 KB

Versions: 13

Compression:

Stored size: 1.2 KB

Contents

module MagLove
  module Tilt
    class CoffeeTemplate < ::Tilt::Template
      self.default_mime_type = 'application/javascript'

      def self.engine_initialized?
        defined? ::CoffeeScript
      end

      def initialize_engine
        require_template_library 'coffee_script'
      end

      def prepare
        options[:bare] = true
      end

      def evaluate(scope, locals, &block)
        @output = CoffeeScript.compile(@data, options)
        
        # handle includes
        @output.gsub(/^include\("([^"]+)"\);$/) do |match|
          path = Regexp.last_match[1]
          path = "#{path}.coffee" if File.extname(path).empty?
          include_path = File.absolute_path(path, File.dirname(file))
          
          # check if base path exists
          if not File.exists?(include_path)
            include_path = File.absolute_path(path, locals[:base_path])
          end
          
          if File.exists?(include_path)
            include_template = ::Tilt.new(include_path)
            include_template.render(nil, locals)
          else
            raise "Path not found: #{include_path}"
          end
        end
        
      end

      def allows_script?
        false
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
maglove-0.6.3 lib/maglove/tilt/coffee_template.rb
maglove-0.6.2 lib/maglove/tilt/coffee_template.rb
maglove-0.6.1 lib/maglove/tilt/coffee_template.rb
maglove-0.6.0 lib/maglove/tilt/coffee_template.rb
maglove-0.5.9 lib/maglove/tilt/coffee_template.rb
maglove-0.5.8 lib/maglove/tilt/coffee_template.rb
maglove-0.5.7 lib/maglove/tilt/coffee_template.rb
maglove-0.5.5 lib/maglove/tilt/coffee_template.rb
maglove-0.5.4 lib/maglove/tilt/coffee_template.rb
maglove-0.5.2 lib/maglove/tilt/coffee_template.rb
maglove-0.5.1 lib/maglove/tilt/coffee_template.rb
maglove-0.5.0 lib/maglove/tilt/coffee_template.rb
maglove-0.3.0 lib/maglove/tilt/coffee_template.rb