Class: Ember::Handlebars::Template

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

included

Constructor Details

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

Returns a new instance of Template.



48
49
50
# File 'lib/ember/handlebars/template.rb', line 48

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



46
47
48
# File 'lib/ember/handlebars/template.rb', line 46

def config
  @config
end

Class Method Details

.call(input) ⇒ Object



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

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

.configObject



18
19
20
# File 'lib/ember/handlebars/template.rb', line 18

def config
  @config ||= Config.new
end

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

Yields:



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

def configure
  yield config
end

.handlebars_available?Boolean

Returns:

  • (Boolean)


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

def handlebars_available?
  Barber::Precompiler.handlebars_available?
end

.instanceObject



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

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

.setup(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/ember/handlebars/template.rb', line 22

def setup(env)
  env.register_mime_type 'text/x-handlebars', extensions: %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: %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: %w(.hbs .hjs .handlebars)
  env.register_transformer 'text/x-ember-handlebars', 'application/javascript', self
end

Instance Method Details

#call(input) ⇒ Object



52
53
54
55
56
57
58
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
# File 'lib/ember/handlebars/template.rb', line 52

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 = actual_name(input)

  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 = {moduleName: module_name}

  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