Class: Sprockets::JstProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/jst_processor.rb

Overview

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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JstProcessor

Returns a new instance of JstProcessor.



36
37
38
# File 'lib/sprockets/jst_processor.rb', line 36

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

Class Method Details

.call(input) ⇒ Object



32
33
34
# File 'lib/sprockets/jst_processor.rb', line 32

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

.default_namespaceObject



21
22
23
# File 'lib/sprockets/jst_processor.rb', line 21

def self.default_namespace
  'this.JST'
end

.instanceObject

Public: Return singleton instance with default options.

Returns JstProcessor object.



28
29
30
# File 'lib/sprockets/jst_processor.rb', line 28

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/sprockets/jst_processor.rb', line 40

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