Class: Hamlbars::Template

Inherits:
Tilt::Template
  • Object
show all
Includes:
Ext::Closure, Ext::Precompiler, Ext::RailsHelper
Defined in:
lib/hamlbars/template.rb

Constant Summary

JS_ESCAPE_MAP =
{
  "\r\n"  => '\n',
  "\n"    => '\n',
  "\r"    => '\n',
  '"'     => '\\"',
  "'"     => "\\'"
}

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Ext::RailsHelper

included

Methods included from Ext::Precompiler

#evaluate_with_precompiler, included

Methods included from Ext::Closure

#evaluate_with_closure, included

Class Method Details

+ (Boolean) engine_initialized?



74
75
76
# File 'lib/hamlbars/template.rb', line 74

def self.engine_initialized?
  defined? ::Haml::Engine
end

+ (Object) path_translator(path)

Used to change the asset path into a string which is safe to use as a JavaScript object property.



23
24
25
# File 'lib/hamlbars/template.rb', line 23

def self.path_translator(path)
  path.downcase.gsub(/[^a-z0-9\/]/, '_')
end

+ (Object) render_templates_for(whom = :handlebars)

Handy helper to preconfigure Hamlbars to render for either :handlebars (default) or :ember.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hamlbars/template.rb', line 29

def self.render_templates_for(whom=:handlebars)
  if whom == :handlebars
    self.template_destination = 'Handlebars.templates'
    self.template_compiler = 'Handlebars.compile'
    self.template_partial_method = 'Handlebars.registerPartial'
  elsif whom == :ember
    self.template_destination = 'Ember.TEMPLATES'
    self.template_compiler = 'Ember.Handlebars.compile'
    self.template_partial_method = 'Ember.Handlebars.registerPartial'
  end
end

+ (Object) template_compiler

The JavaScript function used to compile the HTML string into a usable Handlebars template.



54
55
56
# File 'lib/hamlbars/template.rb', line 54

def self.template_compiler
  @template_compiler ||= 'Handlebars.compile'
end

+ (Object) template_compiler=(x)



58
59
60
# File 'lib/hamlbars/template.rb', line 58

def self.template_compiler=(x)
  @template_compiler = x
end

+ (Object) template_destination

The target object where the rendered template will be stored on the client side. Defaults to 'Handlebars.templates'



44
45
46
# File 'lib/hamlbars/template.rb', line 44

def self.template_destination
  @template_destination ||= 'Handlebars.templates'
end

+ (Object) template_destination=(x)



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

def self.template_destination=(x)
  @template_destination = x
end

+ (Object) template_partial_method

The JavaScript function used on the compile side to register the template as a partial on the client side.



64
65
66
# File 'lib/hamlbars/template.rb', line 64

def self.template_partial_method
  @template_partial_method ||= 'Handlebars.registerPartial'
end

+ (Object) template_partial_method=(x)



68
69
70
# File 'lib/hamlbars/template.rb', line 68

def self.template_partial_method=(x)
  @template_partial_method = x
end

Instance Method Details

- (Object) evaluate(scope, locals, &block)

Uses Haml to render the template into an HTML string, then wraps it in the neccessary JavaScript to serve to the client.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hamlbars/template.rb', line 89

def evaluate(scope, locals, &block)
  template = if @engine.respond_to?(:precompiled_method_return_value, true)
               super(scope, locals, &block)
             else
               @engine.render(scope, locals, &block)
             end

  if scope.respond_to? :logical_path
    path = scope.logical_path
  else
    path = basename
  end

  if basename =~ /^_/
    name = partial_path_translator(path)
    "#{self.class.template_partial_method}('#{name}', '#{template.strip.gsub(/(\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }}');\n"
  else
    name = self.class.path_translator(path)
    "#{self.class.template_destination}[\"#{name}\"] = #{self.class.template_compiler}(\"#{template.strip.gsub(/(\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }}\");\n"
  end
end

- (Object) initialize_engine



78
79
80
# File 'lib/hamlbars/template.rb', line 78

def initialize_engine
  require_template_library 'haml'
end

- (Object) partial_path_translator(path)

Used to change the asset path into a string which is safe to use as a JavaScript object property. When the template is a partial (ie starts with a '_')



114
115
116
117
# File 'lib/hamlbars/template.rb', line 114

def partial_path_translator(path)
  path = remove_underscore_from_partial_path(path)
  self.class.path_translator(path).gsub(%r{/}, '.')
end

- (Object) prepare



82
83
84
85
# File 'lib/hamlbars/template.rb', line 82

def prepare
  options = @options.merge(:filename => eval_file, :line => line)
  @engine = ::Haml::Engine.new(data, options)
end