Sha256: 7fa158def408825b795af3eb893686439ef2372cba059717bf8de4ea80cd884b

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 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(Object.new, locals)
          else
            raise "Path not found: #{include_path}"
          end
        end
        
      end

      def allows_script?
        false
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
maglove-0.8.1 lib/maglove/tilt/coffee_template.rb
maglove-0.8.0 lib/maglove/tilt/coffee_template.rb
maglove-0.7.2 lib/maglove/tilt/coffee_template.rb
maglove-0.7.1 lib/maglove/tilt/coffee_template.rb
maglove-0.7.0 lib/maglove/tilt/coffee_template.rb
maglove-0.6.6 lib/maglove/tilt/coffee_template.rb
maglove-0.6.5 lib/maglove/tilt/coffee_template.rb
maglove-0.6.4 lib/maglove/tilt/coffee_template.rb