Sha256: 4e73f0c894cee06927c245402869427be4266d7816c468a4763842d322f60fc5

Contents?: true

Size: 957 Bytes

Versions: 5

Compression:

Stored size: 957 Bytes

Contents

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

      def prepare; end

      def evaluate(scope, locals, &block)
        # handle includes
        @data = @data.gsub(%r{^//=\srequire\s(.+)$}) do |match|
          path = Regexp.last_match[1]
          include_path = File.absolute_path(path, File.dirname(file))

          # check if base path exists
          unless File.exist?(include_path)
            include_path = File.absolute_path(path, locals[:base_path])
          end

          if File.exist?(include_path)
            include_template = ::Tilt.new(include_path)
            include_template.render(Object.new, locals)
          else
            raise "Path not found: #{include_path}"
          end
        end

        @output = @data
      end

      def allows_script?
        false
      end
    end
  end
end

Tilt.mappings["js"] = [MagLove::Tilt::JsTemplate]

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
maglove-2.0.11 lib/maglove/tilt/js_template.rb
maglove-2.0.10 lib/maglove/tilt/js_template.rb
maglove-2.0.9 lib/maglove/tilt/js_template.rb
maglove-2.0.8 lib/maglove/tilt/js_template.rb
maglove-2.0.7 lib/maglove/tilt/js_template.rb