hogan_assets 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 1.3.0 (2012-06-18)
2
+
3
+ * #9 - Support lambda construct in **mustache**, set via `config.lambda_support = true`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hogan_assets (1.2.0)
4
+ hogan_assets (1.3.0)
5
5
  execjs (>= 1.2.9)
6
6
  sprockets (>= 2.0.3)
7
7
  tilt (>= 1.3.3)
data/README.md CHANGED
@@ -59,6 +59,26 @@ And then execute:
59
59
 
60
60
  $ bundle
61
61
 
62
+ ## Configuration
63
+
64
+ ### Template Extensions
65
+
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:
67
+
68
+ HoganAssets::Config.configure do |config|
69
+ config.template_extensions = %w(mustache hamstache stache)
70
+ end
71
+
72
+ ### Lambda Support
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.
75
+
76
+ *TODO* Should this be on by default?
77
+
78
+ HoganAssets::Config.configure do |config|
79
+ config.lambda_support = true
80
+ end
81
+
62
82
  ## Usage
63
83
 
64
84
  Templates are compiled to a global JavaScript object named `HoganTemplates`. To render `pages/person`:
@@ -74,6 +94,7 @@ I made this because I <3 **mustache** and want to use it in Rails. Follow me on
74
94
  * @mdavidn (Matthew Nelson) : Remove unnecessary template source
75
95
  * @ajacksified (Jack Lawson) : Configurable file extension
76
96
  * @mikesmullin (Mike Smullin) : hamstache support
97
+ * @gleuch (Greg Leuch) : Mustache lambdas
77
98
 
78
99
  ## Contributing
79
100
 
@@ -7,6 +7,7 @@ module HoganAssets
7
7
  #
8
8
  # HoganAssets::Config.configure do |config|
9
9
  # config.template_extensions = ['mustache']
10
+ # config.lambda_support = true
10
11
  # end
11
12
 
12
13
  module Config
@@ -16,7 +17,11 @@ module HoganAssets
16
17
  yield self
17
18
  end
18
19
 
19
- attr_writer :template_extensions
20
+ attr_writer :lambda_support, :template_extensions
21
+
22
+ def lambda_support?
23
+ @lambda_support
24
+ end
20
25
 
21
26
  def template_extensions
22
27
  @template_extensions ||= if haml_available?
@@ -11,17 +11,23 @@ module HoganAssets
11
11
  end
12
12
 
13
13
  def evaluate(scope, locals, &block)
14
+ text = data # Ugly, yes, but to not taint data variable
15
+
14
16
  if scope.pathname.extname == '.hamstache'
15
17
  raise "Unable to complile #{scope.pathname} because haml is not available. Did you add the haml gem?" unless HoganAssets::Config.haml_available?
16
- compiled_template = Haml::Engine.new(data, @options).render
17
- compiled_template = Hogan.compile(compiled_template)
18
+ text = Haml::Engine.new(data, @options).render
19
+ compiled_template = Hogan.compile(source)
18
20
  else
19
21
  compiled_template = Hogan.compile(data)
20
22
  end
23
+
21
24
  template_name = scope.logical_path.inspect
25
+
26
+ # Only emit the source template if we are using lambdas
27
+ text = '' unless HoganAssets::Config.lambda_support?
22
28
  <<-TEMPLATE
23
29
  this.HoganTemplates || (this.HoganTemplates = {});
24
- this.HoganTemplates[#{template_name}] = new Hogan.Template(#{compiled_template});
30
+ this.HoganTemplates[#{template_name}] = new Hogan.Template(#{compiled_template}, #{text.inspect}, Hogan, {});
25
31
  TEMPLATE
26
32
  end
27
33
 
@@ -1,3 +1,3 @@
1
1
  module HoganAssets
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -2,21 +2,32 @@ require 'test_helper'
2
2
 
3
3
  module HoganAssets
4
4
  class TiltTest < Test::Unit::TestCase
5
+ def scope
6
+ Class.new do
7
+ def logical_path ; 'path/to/template' ; end
8
+
9
+ def pathname ; Pathname.new logical_path ; end
10
+ end.new
11
+ end
12
+
5
13
  def test_mime_type
6
14
  assert_equal 'application/javascript', HoganAssets::Tilt.default_mime_type
7
15
  end
8
16
 
9
17
  def test_render
10
- scope = Class.new do
11
- def logical_path ; 'path/to/template' ; end
12
-
13
- def pathname ; Pathname.new logical_path ; end
14
- end.new
18
+ template = HoganAssets::Tilt.new('/myapp/app/assets/templates/path/to/template.mustache') { "This is {{mustache}}" }
19
+ assert_equal <<-END_EXPECTED, template.render(scope, {})
20
+ this.HoganTemplates || (this.HoganTemplates = {});
21
+ 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, {});
22
+ END_EXPECTED
23
+ end
15
24
 
25
+ def test_render_with_lambdas
26
+ HoganAssets::Config.lambda_support = true
16
27
  template = HoganAssets::Tilt.new('/myapp/app/assets/templates/path/to/template.mustache') { "This is {{mustache}}" }
17
28
  assert_equal <<-END_EXPECTED, template.render(scope, {})
18
29
  this.HoganTemplates || (this.HoganTemplates = {});
19
- 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: { }});
30
+ 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, {});
20
31
  END_EXPECTED
21
32
  end
22
33
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'hogan_assets/tilt'
2
2
  require 'hogan_assets/hogan'
3
+ require 'hogan_assets/config'
3
4
 
4
5
  require 'test/unit'
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.2.0
4
+ version: 1.3.0
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-05-31 00:00:00.000000000 Z
12
+ date: 2012-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: execjs
@@ -69,6 +69,7 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
71
  - .rvmrc
72
+ - CHANGELOG.md
72
73
  - Gemfile
73
74
  - Gemfile.lock
74
75
  - LICENSE