Class: Hamlbars::Template
- Inherits:
-
Tilt::Template
- Object
- Tilt::Template
- Hamlbars::Template
- Includes:
- Ext::Closure, Ext::Precompiler, Ext::RailsHelper
- Defined in:
- lib/hamlbars/template.rb
Constant Summary collapse
- JS_ESCAPE_MAP =
{ "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Class Method Summary collapse
- .engine_initialized? ⇒ Boolean
-
.path_translator(path) ⇒ Object
Used to change the asset path into a string which is safe to use as a JavaScript object property.
-
.render_templates_for(whom = :handlebars) ⇒ Object
Handy helper to preconfigure Hamlbars to render for either :handlebars (default) or :ember.
-
.template_compiler ⇒ Object
The JavaScript function used to compile the HTML string into a usable Handlebars template.
- .template_compiler=(x) ⇒ Object
-
.template_destination ⇒ Object
The target object where the rendered template will be stored on the client side.
- .template_destination=(x) ⇒ Object
-
.template_partial_method ⇒ Object
The JavaScript function used on the compile side to register the template as a partial on the client side.
- .template_partial_method=(x) ⇒ Object
Instance Method Summary collapse
-
#evaluate(scope, locals, &block) ⇒ Object
Uses Haml to render the template into an HTML string, then wraps it in the neccessary JavaScript to serve to the client.
- #initialize_engine ⇒ Object
-
#partial_path_translator(path) ⇒ Object
Used to change the asset path into a string which is safe to use as a JavaScript object property.
- #prepare ⇒ Object
Methods included from Ext::RailsHelper
Methods included from Ext::Precompiler
#evaluate_with_precompiler, included
Methods included from Ext::Closure
#evaluate_with_closure, included
Class Method Details
.engine_initialized? ⇒ Boolean
74 75 76 |
# File 'lib/hamlbars/template.rb', line 74 def self.engine_initialized? defined? ::Haml::Engine end |
.path_translator(path) ⇒ Object
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 |
.render_templates_for(whom = :handlebars) ⇒ Object
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 |
.template_compiler ⇒ Object
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 |
.template_compiler=(x) ⇒ Object
58 59 60 |
# File 'lib/hamlbars/template.rb', line 58 def self.template_compiler=(x) @template_compiler = x end |
.template_destination ⇒ Object
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 |
.template_destination=(x) ⇒ Object
48 49 50 |
# File 'lib/hamlbars/template.rb', line 48 def self.template_destination=(x) @template_destination = x end |
.template_partial_method ⇒ Object
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 |
.template_partial_method=(x) ⇒ Object
68 69 70 |
# File 'lib/hamlbars/template.rb', line 68 def self.template_partial_method=(x) @template_partial_method = x end |
Instance Method Details
#evaluate(scope, locals, &block) ⇒ Object
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 |
#initialize_engine ⇒ Object
78 79 80 |
# File 'lib/hamlbars/template.rb', line 78 def initialize_engine require_template_library 'haml' end |
#partial_path_translator(path) ⇒ Object
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 |
#prepare ⇒ Object
82 83 84 85 |
# File 'lib/hamlbars/template.rb', line 82 def prepare = @options.merge(:filename => eval_file, :line => line) @engine = ::Haml::Engine.new(data, ) end |