Class: Sprockets::JstProcessor

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

Overview

Public: JST transformer.

Exports server side compiled templates to an object.

Name your template “users/show.ejs”, “users/new.eco”, etc.

To accept the default options

environment.register_transformer
  'application/javascript+function',
  'application/javascript', JstProcessor

Change the default namespace.

environment.register_transformer
  'application/javascript+function',
  'application/javascript', JstProcessor.new(namespace: 'App.templates')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace: self.class.default_namespace) ⇒ JstProcessor

Returns a new instance of JstProcessor.



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

def initialize(namespace: self.class.default_namespace)
  @namespace = namespace
end

Class Method Details

.call(input) ⇒ Object



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

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

.default_namespaceObject



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

def self.default_namespace
  'this.JST'
end

.instanceObject

Public: Return singleton instance with default options.

Returns JstProcessor object.



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

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



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

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