Skip to content

Commit

Permalink
add custom template namespace support
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstrickland committed Sep 10, 2012
1 parent 8924d63 commit c78ddd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/hogan_assets/config.rb
Expand Up @@ -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
Expand All @@ -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']
Expand Down
5 changes: 3 additions & 2 deletions lib/hogan_assets/tilt.rb
Expand Up @@ -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?
Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions test/hogan_assets/tilt_test.rb
Expand Up @@ -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

0 comments on commit c78ddd0

Please sign in to comment.