hogan_assets 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ## 1.3.2 (2012-08-04)
2
+
3
+ * Use `HoganAssets::Config.path\_prefix` to strip a common path prefix from your template names
4
+
1
5
  ## 1.3.1 (2012-06-21)
2
6
 
3
7
  * #11 - Fix **hamstache** support, broken in 1.3.0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hogan_assets (1.3.1)
4
+ hogan_assets (1.3.2)
5
5
  execjs (>= 1.2.9)
6
6
  sprockets (>= 2.0.3)
7
7
  tilt (>= 1.3.3)
data/README.md CHANGED
@@ -29,7 +29,7 @@ Require your templates with `require_tree`:
29
29
 
30
30
  //= require_tree ./templates
31
31
 
32
- Templates are named for the sub-path from your manifest with `require_tree`. For example, the file `app/assets/javascripts/templates/pages/person.mustache` will be named `templates/pages/person`. _(TODO: make this nicer)_
32
+ Templates are named for the sub-path from your manifest with `require_tree`. For example, the file `app/assets/javascripts/templates/pages/person.mustache` will be named `templates/pages/person`. See `path_prefix` below!
33
33
 
34
34
  ### Installation with sprockets
35
35
 
@@ -43,8 +43,6 @@ And then execute:
43
43
 
44
44
  Require `hogan.js` somewhere in your JavaScript.
45
45
 
46
- *TODO* Templates?
47
-
48
46
  ## Hamstache!
49
47
 
50
48
  _hamstache_ is the quite popular combination of `haml` and `mustache`, a more robust solution exists using [haml_assets](https://github.com/infbio/haml_assets), but if all you want is nicer markup, you need to take these two additional steps:
@@ -61,22 +59,38 @@ And then execute:
61
59
 
62
60
  ## Configuration
63
61
 
64
- ### Template Extensions
62
+ ### Lambda Support
65
63
 
66
- **HoganAssets** recognizes templates ending in `.mustache` and if you have haml available, `.hamstache`. You can change the template extensions by setting the `template_extensions` configuration option in an initializer:
64
+ **mustache** lambdas are off by default. (Not sure what that is? Read the [mustache](http://mustache.github.com/mustache.5.html) man page!) If you want them on, set the `lambda_support` option to true. This will include the raw template text as part of the compiled template; each template will be correspondingly larger. *TODO* Should this be on by default?
67
65
 
68
66
  HoganAssets::Config.configure do |config|
69
- config.template_extensions = %w(mustache hamstache stache)
67
+ config.lambda_support = true
70
68
  end
71
69
 
72
- ### Lambda Support
70
+ ### Path Prefix
71
+
72
+ You can strip a prefix from your template names. For example, when using Rails, if you place your templates in `app/assets/javascripts/app/templates` and organize them like Rails views (i.e. `posts/index.mustache`); then the `index.mustache` template gets compiled into:
73
73
 
74
- **HoganAssets** supports **mustache** lambdas. Set the `lambda_support` option to true to enable lambdas for your templates. This will include the raw template text as part of the compiled template; each template will be correspondingly larger.
74
+ HoganTemplates['app/templates/posts/index']
75
75
 
76
- *TODO* Should this be on by default?
76
+ You can strip the common part of the template name by setting the `path_prefix` option. For example:
77
77
 
78
78
  HoganAssets::Config.configure do |config|
79
- config.lambda_support = true
79
+ config.path_prefix = 'app/templates'
80
+ end
81
+
82
+ will give you a compiled template:
83
+
84
+ HoganTemplates['posts/index']
85
+
86
+ *TODO* Can this be done in a nicer way?
87
+
88
+ ### Template Extensions
89
+
90
+ By default, templates are recognized if they have an extension of `.mustache` (and if you have haml available, `.hamstache`.) You can change the template extensions by setting the `template_extensions` configuration option in an initializer:
91
+
92
+ HoganAssets::Config.configure do |config|
93
+ config.template_extensions = %w(mustache hamstache stache)
80
94
  end
81
95
 
82
96
  ## Usage
@@ -6,23 +6,32 @@ module HoganAssets
6
6
  # Or in a block:
7
7
  #
8
8
  # HoganAssets::Config.configure do |config|
9
- # config.template_extensions = ['mustache']
10
- # config.lambda_support = true
9
+ # config.lambda_support = false
10
+ # config.path_prefix = 'templates'
11
+ # config.template_extensions = ['mustache', 'hamstache']
11
12
  # end
12
-
13
+ #
13
14
  module Config
14
15
  extend self
15
16
 
17
+ attr_writer :lambda_support, :path_prefix, :template_extensions
18
+
16
19
  def configure
17
20
  yield self
18
21
  end
19
22
 
20
- attr_writer :lambda_support, :template_extensions
23
+ def haml_available?
24
+ defined? ::Haml::Engine
25
+ end
21
26
 
22
27
  def lambda_support?
23
28
  @lambda_support
24
29
  end
25
30
 
31
+ def path_prefix
32
+ @path_prefix ||= 'templates'
33
+ end
34
+
26
35
  def template_extensions
27
36
  @template_extensions ||= if haml_available?
28
37
  ['mustache', 'hamstache']
@@ -30,9 +39,5 @@ module HoganAssets
30
39
  ['mustache']
31
40
  end
32
41
  end
33
-
34
- def haml_available?
35
- defined? ::Haml::Engine
36
- end
37
42
  end
38
43
  end
@@ -11,8 +11,10 @@ module HoganAssets
11
11
  end
12
12
 
13
13
  def evaluate(scope, locals, &block)
14
- text = if scope.pathname.extname == '.hamstache'
15
- raise "Unable to complile #{scope.pathname} because haml is not available. Did you add the haml gem?" unless HoganAssets::Config.haml_available?
14
+ template_path = TemplatePath.new scope
15
+
16
+ text = if template_path.is_hamstache?
17
+ raise "Unable to complile #{template_path.full_path} because haml is not available. Did you add the haml gem?" unless HoganAssets::Config.haml_available?
16
18
  Haml::Engine.new(data, @options).render
17
19
  else
18
20
  data
@@ -25,12 +27,37 @@ module HoganAssets
25
27
  text = '' unless HoganAssets::Config.lambda_support?
26
28
  <<-TEMPLATE
27
29
  this.HoganTemplates || (this.HoganTemplates = {});
28
- this.HoganTemplates[#{template_name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {});
30
+ this.HoganTemplates[#{template_path.name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {});
29
31
  TEMPLATE
30
32
  end
31
33
 
32
34
  protected
33
35
 
34
36
  def prepare; end
37
+
38
+ class TemplatePath
39
+ attr_accessor :full_path
40
+
41
+ def initialize(scope)
42
+ self.template_path = scope.logical_path
43
+ self.full_path = scope.pathname
44
+ end
45
+
46
+ def is_hamstache?
47
+ full_path.to_s.end_with? '.hamstache'
48
+ end
49
+
50
+ def name
51
+ @name ||= relative_path.dump
52
+ end
53
+
54
+ private
55
+
56
+ attr_accessor :template_path
57
+
58
+ def relative_path
59
+ @relative_path ||= template_path.gsub(/^#{HoganAssets::Config.path_prefix}\/(.*)$/i, "\\1")
60
+ end
61
+ end
35
62
  end
36
63
  end
@@ -1,3 +1,3 @@
1
1
  module HoganAssets
2
- VERSION = "1.3.1"
2
+ VERSION = "1.3.2"
3
3
  end
@@ -2,16 +2,22 @@ require 'test_helper'
2
2
 
3
3
  module HoganAssets
4
4
  class TiltTest < Test::Unit::TestCase
5
- def scope(path)
6
- instance = Class.new do
7
- attr_accessor :_path
5
+ # Try to act like sprockets.
6
+ def make_scope(root, file)
7
+ Class.new do
8
+ define_method(:logical_path) { pathname.to_s.gsub(root + '/', '').gsub(/\..*/, '') }
8
9
 
9
- def logical_path ; _path.gsub /\..*/, '' ; end
10
+ define_method(:pathname) { Pathname.new(root) + file }
10
11
 
11
- def pathname ; Pathname.new _path ; end
12
+ define_method(:root_path) { root }
13
+
14
+ define_method(:s_path) { pathname.to_s }
12
15
  end.new
13
- instance._path = path
14
- instance
16
+ end
17
+
18
+ def teardown
19
+ HoganAssets::Config.lambda_support = false
20
+ HoganAssets::Config.path_prefix = 'templates'
15
21
  end
16
22
 
17
23
  def test_mime_type
@@ -19,31 +25,55 @@ module HoganAssets
19
25
  end
20
26
 
21
27
  def test_render
22
- path = 'path/to/template.mustache'
23
- template = HoganAssets::Tilt.new(path) { "This is {{mustache}}" }
24
- assert_equal <<-END_EXPECTED, template.render(scope(path), {})
28
+ scope = make_scope '/myapp/app/assets/javascripts', 'path/to/template.mustache'
29
+
30
+ template = HoganAssets::Tilt.new(scope.s_path) { "This is {{mustache}}" }
31
+
32
+ assert_equal <<-END_EXPECTED, template.render(scope, {})
25
33
  this.HoganTemplates || (this.HoganTemplates = {});
26
34
  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: { }}, "", Hogan, {});
27
35
  END_EXPECTED
28
36
  end
29
37
 
30
38
  def test_hamstache_render
31
- path = 'path/to/template.hamstache'
32
- template = HoganAssets::Tilt.new(path) { "%p This is {{hamstache}}" }
33
- assert_equal <<-END_EXPECTED, template.render(scope(path), {})
39
+ scope = make_scope '/myapp/app/assets/javascripts', 'path/to/template.hamstache'
40
+
41
+ template = HoganAssets::Tilt.new(scope.s_path) { "%p This is {{hamstache}}" }
42
+
43
+ assert_equal <<-END_EXPECTED, template.render(scope, {})
34
44
  this.HoganTemplates || (this.HoganTemplates = {});
35
45
  this.HoganTemplates[\"path/to/template\"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||\"\");t.b(\"<p>This is \");t.b(t.v(t.f(\"hamstache\",c,p,0)));t.b(\"</p>\");t.b(\"\\n\");return t.fl(); },partials: {}, subs: { }}, \"\", Hogan, {});
36
46
  END_EXPECTED
37
47
  end
38
48
 
39
49
  def test_render_with_lambdas
40
- path = 'path/to/template.mustache'
41
- HoganAssets::Config.lambda_support = true
42
- template = HoganAssets::Tilt.new(path) { "This is {{mustache}}" }
43
- assert_equal <<-END_EXPECTED, template.render(scope(path), {})
50
+ HoganAssets::Config.configure do |config|
51
+ config.lambda_support = true
52
+ end
53
+
54
+ scope = make_scope '/myapp/app/assets/javascripts', 'path/to/template.mustache'
55
+
56
+ template = HoganAssets::Tilt.new(scope.s_path) { "This is {{mustache}}" }
57
+
58
+ assert_equal <<-END_EXPECTED, template.render(scope, {})
44
59
  this.HoganTemplates || (this.HoganTemplates = {});
45
60
  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, {});
46
61
  END_EXPECTED
47
62
  end
63
+
64
+ def test_path_prefix
65
+ HoganAssets::Config.configure do |config|
66
+ config.path_prefix = 'app/templates'
67
+ end
68
+
69
+ scope = make_scope '/myapp/app/assets/javascripts', 'app/templates/template.mustache'
70
+
71
+ template = HoganAssets::Tilt.new(scope.s_path) { "This is {{mustache}}" }
72
+
73
+ assert_equal <<-END_EXPECTED, template.render(scope, {})
74
+ this.HoganTemplates || (this.HoganTemplates = {});
75
+ 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, {});
76
+ END_EXPECTED
77
+ end
48
78
  end
49
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hogan_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-21 00:00:00.000000000 Z
12
+ date: 2012-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: execjs