Sha256: c8d6682ef5468b32d9b077a6ec0195222ec229c989c15fff138ca22c6425578a

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'tilt'

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

    def initialize_engine
      require_template_library 'haml'
    rescue LoadError
      # haml not available
    end

    def evaluate(scope, locals, &block)
      text = data # Ugly, yes, but to not taint data variable

      if scope.pathname.extname == '.hamstache'
        raise "Unable to complile #{scope.pathname} because haml is not available. Did you add the haml gem?" unless HoganAssets::Config.haml_available?
        text = Haml::Engine.new(data, @options).render
        compiled_template = Hogan.compile(source)
      else
        compiled_template = Hogan.compile(data)
      end

      template_name = scope.logical_path.inspect

      # Only emit the source template if we are using lambdas
      text = '' unless HoganAssets::Config.lambda_support?
      <<-TEMPLATE
        this.HoganTemplates || (this.HoganTemplates = {});
        this.HoganTemplates[#{template_name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {});
      TEMPLATE
    end

    protected

    def prepare; end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hogan_assets-1.3.0 lib/hogan_assets/tilt.rb