Sha256: 9c3f4f97e71af377ede93b03b0edc6ce450743e0a95c5a18d4196e25eba33d81

Contents?: true

Size: 1.16 KB

Versions: 83

Compression:

Stored size: 1.16 KB

Contents

module Sprockets
  # Public: .jst engine.
  #
  # Exports server side compiled templates to an object.
  #
  # Name your template "users/show.jst.ejs", "users/new.jst.eco", etc.
  #
  # To accept the default options
  #
  #     environment.register_engine '.jst',
  #       JstProcessor,
  #       mime_type: 'application/javascript'
  #
  # Change the default namespace.
  #
  #     environment.register_engine '.jst',
  #       JstProcessor.new(namespace: 'App.templates'),
  #       mime_type: 'application/javascript'
  #
  class JstProcessor
    def self.default_namespace
      'this.JST'
    end

    # Public: Return singleton instance with default options.
    #
    # Returns JstProcessor object.
    def self.instance
      @instance ||= new
    end

    def self.call(input)
      instance.call(input)
    end

    def initialize(options = {})
      @namespace = options[:namespace] || self.class.default_namespace
    end

    def call(input)
      data = input[:data].gsub(/$(.)/m, "\\1  ").strip
      key  = input[:name]
      <<-JST
(function() { #{@namespace} || (#{@namespace} = {}); #{@namespace}[#{key.inspect}] = #{data};
}).call(this);
      JST
    end
  end
end

Version data entries

83 entries across 77 versions & 15 rubygems

Version Path
sprockets-3.0.0.beta.9 lib/sprockets/jst_processor.rb
sprockets-3.0.0.beta.8 lib/sprockets/jst_processor.rb
sprockets-3.0.0.beta.7 lib/sprockets/jst_processor.rb