From c78ddd03b1bea71210e30f0f6fe98d8737ae2a9a Mon Sep 17 00:00:00 2001 From: Adam Strickland Date: Mon, 10 Sep 2012 17:28:22 -0500 Subject: [PATCH] add custom template namespace support --- lib/hogan_assets/config.rb | 6 +++++- lib/hogan_assets/tilt.rb | 5 +++-- test/hogan_assets/tilt_test.rb | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/hogan_assets/config.rb b/lib/hogan_assets/config.rb index 45ee89b..bef63c6 100644 --- a/lib/hogan_assets/config.rb +++ b/lib/hogan_assets/config.rb @@ -14,7 +14,7 @@ module HoganAssets module Config extend self - attr_writer :lambda_support, :path_prefix, :template_extensions + attr_writer :lambda_support, :path_prefix, :template_extensions, :template_namespace def configure yield self @@ -32,6 +32,10 @@ def path_prefix @path_prefix ||= 'templates' end + def template_namespace + @template_namespace ||= 'HoganTemplates' + end + def template_extensions @template_extensions ||= if haml_available? ['mustache', 'hamstache'] diff --git a/lib/hogan_assets/tilt.rb b/lib/hogan_assets/tilt.rb index 5f4332e..35e6172 100644 --- a/lib/hogan_assets/tilt.rb +++ b/lib/hogan_assets/tilt.rb @@ -12,6 +12,7 @@ def initialize_engine def evaluate(scope, locals, &block) template_path = TemplatePath.new scope + template_namespace = HoganAssets::Config.template_namespace text = if template_path.is_hamstache? raise "Unable to complile #{template_path.full_path} because haml is not available. Did you add the haml gem?" unless HoganAssets::Config.haml_available? @@ -26,8 +27,8 @@ def evaluate(scope, locals, &block) # Only emit the source template if we are using lambdas text = '' unless HoganAssets::Config.lambda_support? <<-TEMPLATE - this.HoganTemplates || (this.HoganTemplates = {}); - this.HoganTemplates[#{template_path.name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {}); + this.#{template_namespace} || (this.#{template_namespace} = {}); + this.#{template_namespace}[#{template_path.name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {}); TEMPLATE end diff --git a/test/hogan_assets/tilt_test.rb b/test/hogan_assets/tilt_test.rb index ee5e53b..af1d686 100644 --- a/test/hogan_assets/tilt_test.rb +++ b/test/hogan_assets/tilt_test.rb @@ -75,5 +75,20 @@ def test_path_prefix this.HoganTemplates[\"template\"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||\"\");t.b(\"This is \");t.b(t.v(t.f(\"mustache\",c,p,0)));return t.fl(); },partials: {}, subs: { }}, "", Hogan, {}); END_EXPECTED end + + def test_template_namespace + HoganAssets::Config.configure do |config| + config.template_namespace = 'JST' + end + + scope = make_scope '/myapp/app/assets/javascripts', 'path/to/template.mustache' + + template = HoganAssets::Tilt.new(scope.s_path) { "This is {{mustache}}" } + + assert_equal <<-END_EXPECTED, template.render(scope, {}) + this.JST || (this.JST = {}); + this.JST[\"path/to/template\"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||\"\");t.b(\"This is \");t.b(t.v(t.f(\"mustache\",c,p,0)));return t.fl(); },partials: {}, subs: { }}, "", Hogan, {}); + END_EXPECTED + end end end