Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #28 from spikebrehm/master

Adding template_namespace configuration option
  • Loading branch information...
commit f410171f677b0260f93ff76fd61afe52d27192bb 2 parents 92f139e + d30bee5
Les Hill authored
4 CHANGELOG.md
View
@@ -1,3 +1,7 @@
+## 0.6.3 (2012-08-22)
+
+* Added ability to change client-side template namespace from `HandlebarsTemplates` using `template_namespace` config option
+
## 0.6.2 (2012-07-27)
* Added support for knownHelpers and knownHelperOnly compiler options
8 Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- handlebars_assets (0.6.2)
+ handlebars_assets (0.6.3)
execjs (>= 1.2.9)
sprockets (>= 2.0.3)
tilt
@@ -9,13 +9,13 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- execjs (1.3.0)
+ execjs (1.4.0)
multi_json (~> 1.0)
hike (1.2.1)
- multi_json (1.2.0)
+ multi_json (1.3.6)
rack (1.4.1)
rake (0.9.2.2)
- sprockets (2.4.0)
+ sprockets (2.4.3)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
8 README.md
View
@@ -81,6 +81,14 @@ You can then invoke the resulting template in your application's Javascript
HandlebarsTemplates['contacts/new'](context);
+## The template namespace
+
+By default, the global Javascript object that holds the compiled templates is `HandlebarsTemplates`, but it can
+be easily renamed. Another common template namespace is `JST`. Just change the `template_namespace` configuration option
+when you initialize your application.
+
+ HandlebarsAssets::Config.template_namespace = 'JST'
+
## Partials
If you begin the name of the template with an underscore, it will be recognized as a partial. You can invoke partials inside a template using the Handlebars partial syntax:
6 lib/handlebars_assets/config.rb
View
@@ -5,7 +5,7 @@ module HandlebarsAssets
module Config
extend self
- attr_writer :known_helpers, :known_helpers_only, :path_prefix
+ attr_writer :known_helpers, :known_helpers_only, :path_prefix, :template_namespace
def known_helpers
@known_helpers || []
@@ -26,6 +26,10 @@ def path_prefix
@path_prefix ||= 'templates'
end
+ def template_namespace
+ @template_namespace || 'HandlebarsTemplates'
+ end
+
private
def generate_known_helpers_hash
8 lib/handlebars_assets/tilt_handlebars.rb
View
@@ -11,6 +11,8 @@ def evaluate(scope, locals, &block)
compiled_hbs = Handlebars.precompile(data, HandlebarsAssets::Config.options)
+ template_namespace = HandlebarsAssets::Config.template_namespace
+
if template_path.is_partial?
<<-PARTIAL
(function() {
@@ -20,9 +22,9 @@ def evaluate(scope, locals, &block)
else
<<-TEMPLATE
(function() {
- this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
- this.HandlebarsTemplates[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
- return HandlebarsTemplates[#{template_path.name}];
+ this.#{template_namespace} || (this.#{template_namespace} = {});
+ this.#{template_namespace}[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
+ return #{template_namespace}[#{template_path.name}];
}).call(this);
TEMPLATE
end
2  lib/handlebars_assets/version.rb
View
@@ -1,3 +1,3 @@
module HandlebarsAssets
- VERSION = "0.6.2"
+ VERSION = "0.6.3"
end
34 test/handlebars_assets/tilt_handlebars_test.rb
View
@@ -207,10 +207,32 @@ def hbs_custom_compiled_with_helper_opt(template_name, helper_name)
END_EXPECTED
end
+ def hbs_compiled_template_namespace(template_name)
+ <<END_EXPECTED
+ (function() {
+ this.JST || (this.JST = {});
+ this.JST["#{template_name}"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers;
+ var buffer = "", stack1, foundHelper, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
+
+
+ buffer += "This is ";
+ foundHelper = helpers.handlebars;
+ stack1 = foundHelper || depth0.handlebars;
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "handlebars", { hash: {} }); }
+ buffer += escapeExpression(stack1);
+ return buffer;});
+ return JST["#{template_name}"];
+ }).call(this);
+END_EXPECTED
+ end
+
def teardown
HandlebarsAssets::Config.known_helpers = []
HandlebarsAssets::Config.known_helpers_only = false
HandlebarsAssets::Config.path_prefix = nil
+ HandlebarsAssets::Config.template_namespace = nil
end
def test_render
@@ -309,5 +331,17 @@ def test_with_custom_known_helpers
assert_equal hbs_custom_compiled_with_helper_opt('test_custom_known_helper', 'custom'), template.render(scope, {})
end
+
+ def test_template_namespace
+ root = '/myapp/app/assets/javascripts'
+ file = 'test_template_namespace.hbs'
+ scope = make_scope root, file
+
+ HandlebarsAssets::Config.template_namespace = 'JST'
+
+ template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "This is {{handlebars}}" }
+
+ assert_equal hbs_compiled_template_namespace('test_template_namespace'), template.render(scope, {})
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.