Class: Ember::Handlebars::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ember/handlebars/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = self.class.config.dup) ⇒ Template

Returns a new instance of Template.



55
56
57
# File 'lib/ember/handlebars/template.rb', line 55

def initialize(config = self.class.config.dup)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



53
54
55
# File 'lib/ember/handlebars/template.rb', line 53

def config
  @config
end

Class Method Details

.call(input) ⇒ Object



38
39
40
# File 'lib/ember/handlebars/template.rb', line 38

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

.configObject



15
16
17
# File 'lib/ember/handlebars/template.rb', line 15

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



11
12
13
# File 'lib/ember/handlebars/template.rb', line 11

def configure
  yield config
end

.handlebars_available?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ember/handlebars/template.rb', line 42

def handlebars_available?
  Barber::Precompiler.handlebars_available?
end

.instanceObject



34
35
36
# File 'lib/ember/handlebars/template.rb', line 34

def instance
  @instance ||= new(config)
end

.setup(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/ember/handlebars/template.rb', line 19

def setup(env)
  env.register_mime_type 'text/x-handlebars', extensions: with_js_extension(%w(.raw.hbs .raw.hjs .raw.handlebars))
  env.register_transformer 'text/x-handlebars', 'application/javascript', self

  env.register_mime_type 'text/x-ember-mustache', extensions: with_js_extension(%w(.mustache.hbs .mustache.hjs .mustache.handlebars))
  env.register_transformer 'text/x-ember-mustache', 'application/javascript', self

  env.register_mime_type 'text/x-ember-handlebars', extensions: with_js_extension(%w(.hbs .hjs .handlebars))
  env.register_transformer 'text/x-ember-handlebars', 'application/javascript', self
end

.setup_ember_template_compiler(path) ⇒ Object



30
31
32
# File 'lib/ember/handlebars/template.rb', line 30

def setup_ember_template_compiler(path)
  Barber::Ember::Precompiler.ember_template_compiler_path = path
end

Instance Method Details

#call(input) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ember/handlebars/template.rb', line 59

def call(input)
  data = input[:data]
  filename = input[:filename]

  raw = handlebars?(filename)

  if raw
    template = data
  else
    template = mustache_to_handlebars(filename, data)
  end

  template_name = input[:name]

  module_name =
    case config.output_type
    when :amd
      amd_template_target(config.amd_namespace, template_name)
    when :global
      template_path(template_name, config)
    else
      raise "Unsupported `output_type`: #{config.output_type}"
    end

  meta = meta_supported? ? {moduleName: module_name} : false

  if config.precompile
    if raw
      template = precompile_handlebars(template, input)
    else
      template = precompile_ember_handlebars(template, config.ember_template, input, meta)
    end
  else
    if raw
      template = compile_handlebars(data)
    else
      template = compile_ember_handlebars(template, config.ember_template, meta)
    end
  end

  case config.output_type
  when :amd
    "define('#{module_name}', ['exports'], function(__exports__){ __exports__['default'] = #{template} });"
  when :global
    namespace = raw ? config.raw_template_namespace : 'Ember.TEMPLATES'
    target = global_template_target(namespace, template_name, config)

    "#{target} = #{template}\n"
  else
    raise "Unsupported `output_type`: #{config.output_type}"
  end
end