Skip to content
Browse files

Add #path_prefix configuration option

Use #path_prefix to strip off a prefix from your template names. For
example, given:

    # require 'app'

Then a template file located at .../app/views/posts/post.mustache will
be given a template name of 'app/views/posts/post'.

Use a #path_prefix of 'app/views' to make the name be 'posts/post'.
  • Loading branch information...
1 parent ffaa2bc commit ea0112b922846eee5ed52f851a97d77d31826136 @leshill committed Aug 4, 2012
Showing with 59 additions and 11 deletions.
  1. +13 −8 lib/hogan_assets/config.rb
  2. +30 −3 lib/hogan_assets/tilt.rb
  3. +16 −0 test/hogan_assets/tilt_test.rb
View
21 lib/hogan_assets/config.rb
@@ -6,33 +6,38 @@ module HoganAssets
# Or in a block:
#
# HoganAssets::Config.configure do |config|
- # config.template_extensions = ['mustache']
- # config.lambda_support = true
+ # config.lambda_support = false
+ # config.path_prefix = 'templates'
+ # config.template_extensions = ['mustache', 'hamstache']
# end
-
+ #
module Config
extend self
+ attr_writer :lambda_support, :path_prefix, :template_extensions
+
def configure
yield self
end
- attr_writer :lambda_support, :template_extensions
+ def haml_available?
+ defined? ::Haml::Engine
+ end
def lambda_support?
@lambda_support
end
+ def path_prefix
+ @path_prefix ||= 'templates'
+ end
+
def template_extensions
@template_extensions ||= if haml_available?
['mustache', 'hamstache']
else
['mustache']
end
end
-
- def haml_available?
- defined? ::Haml::Engine
- end
end
end
View
33 lib/hogan_assets/tilt.rb
@@ -11,8 +11,10 @@ def initialize_engine
end
def evaluate(scope, locals, &block)
- text = if scope.pathname.extname == '.hamstache'
- raise "Unable to complile #{scope.pathname} because haml is not available. Did you add the haml gem?" unless HoganAssets::Config.haml_available?
+ template_path = TemplatePath.new scope
+
+ 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?
Haml::Engine.new(data, @options).render
else
data
@@ -25,12 +27,37 @@ def evaluate(scope, locals, &block)
text = '' unless HoganAssets::Config.lambda_support?
<<-TEMPLATE
this.HoganTemplates || (this.HoganTemplates = {});
- this.HoganTemplates[#{template_name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {});
+ this.HoganTemplates[#{template_path.name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {});
TEMPLATE
end
protected
def prepare; end
+
+ class TemplatePath
+ attr_accessor :full_path
+
+ def initialize(scope)
+ self.template_path = scope.logical_path
+ self.full_path = scope.pathname
+ end
+
+ def is_hamstache?
+ full_path.to_s.end_with? '.hamstache'
+ end
+
+ def name
+ @name ||= relative_path.dump
+ end
+
+ private
+
+ attr_accessor :template_path
+
+ def relative_path
+ @relative_path ||= template_path.gsub(/^#{HoganAssets::Config.path_prefix}\/(.*)$/i, "\\1")
+ end
+ end
end
end
View
16 test/hogan_assets/tilt_test.rb
@@ -17,6 +17,7 @@ def make_scope(root, file)
def teardown
HoganAssets::Config.lambda_support = false
+ HoganAssets::Config.path_prefix = 'templates'
end
def test_mime_type
@@ -59,5 +60,20 @@ def test_render_with_lambdas
this.HoganTemplates[\"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: { }}, "This is {{mustache}}", Hogan, {});
END_EXPECTED
end
+
+ def test_path_prefix
+ HoganAssets::Config.configure do |config|
+ config.path_prefix = 'app/templates'
+ end
+
+ scope = make_scope '/myapp/app/assets/javascripts', 'app/templates/template.mustache'
+
+ template = HoganAssets::Tilt.new(scope.s_path) { "This is {{mustache}}" }
+
+ assert_equal <<-END_EXPECTED, template.render(scope, {})
+ this.HoganTemplates || (this.HoganTemplates = {});
+ 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
end
end

0 comments on commit ea0112b

Please sign in to comment.
Something went wrong with that request. Please try again.