handlebars_assets 0.6.7 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.7.0 (2012-11-16)
2
+
3
+ * Support `.hamlbars` extension and Haml
4
+
1
5
  ## 0.6.7 (2012-11-11)
2
6
 
3
7
  * Support `.handlebars` extension - @BlakeWilliams
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- handlebars_assets (0.6.7)
4
+ handlebars_assets (0.7.0)
5
5
  execjs (>= 1.2.9)
6
6
  sprockets (>= 2.0.3)
7
7
  tilt
@@ -11,6 +11,7 @@ GEM
11
11
  specs:
12
12
  execjs (1.4.0)
13
13
  multi_json (~> 1.0)
14
+ haml (3.1.7)
14
15
  hike (1.2.1)
15
16
  multi_json (1.3.6)
16
17
  rack (1.4.1)
@@ -26,5 +27,6 @@ PLATFORMS
26
27
  ruby
27
28
 
28
29
  DEPENDENCIES
30
+ haml
29
31
  handlebars_assets!
30
32
  rake
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Use handlebars.js templates with the asset pipeline and sprockets
2
2
 
3
- Are your `handlebars.js` templates littering your Rails views with `script` tags? Wondering why the nifty Rails 3.1 asset pipeline streamlines all your Javascript except for your Handlebars templates? Wouldn't it be nice to have your Handlebars templates compiled, compressed, and cached like your other Javascript?
3
+ Are your `handlebars.js` templates littering your Rails views with `script` tags? Wondering why the nifty Rails 3.1 asset pipeline streamlines all your JavaScript except for your Handlebars templates? Wouldn't it be nice to have your Handlebars templates compiled, compressed, and cached like your other JavaScript?
4
4
 
5
5
  Yea, I think so too. That is why I wrote **handlebars_assets**. Give your Handlebars templates their own files (including partials) and have them compiled, compressed, and cached as part of the Rails 3.1 asset pipeline!
6
6
 
@@ -36,13 +36,13 @@ for the next steps to work.
36
36
  env.append_path HandlebarsAssets.path
37
37
 
38
38
 
39
- # Compiling your Javascript templates in the Rails asset pipeline
39
+ # Compiling your JavaScript templates in the Rails asset pipeline
40
40
 
41
- Require `handlebars.runtime.js` in your Javascript manifest (i.e. `application.js`)
41
+ Require `handlebars.runtime.js` in your JavaScript manifest (i.e. `application.js`)
42
42
 
43
43
  //= require handlebars.runtime
44
44
 
45
- If you need to compile your Javascript templates in the browser as well, you should instead require `handlebars.js` (which is significantly larger)
45
+ If you need to compile your JavaScript templates in the browser as well, you should instead require `handlebars.js` (which is significantly larger)
46
46
 
47
47
  //= require handlebars
48
48
 
@@ -54,7 +54,7 @@ If you need to compile your Javascript templates in the browser as well, you sho
54
54
 
55
55
  ## Templates directory
56
56
 
57
- You should locate your templates with your other assets, for example `app/assets/javascripts/templates`. In your Javascript manifest file, use `require_tree` to pull in the templates
57
+ You should locate your templates with your other assets, for example `app/assets/javascripts/templates`. In your JavaScript manifest file, use `require_tree` to pull in the templates
58
58
 
59
59
  //= require_tree ./templates
60
60
 
@@ -72,23 +72,39 @@ For example, if you have new, edit, and show templates for a Contact model
72
72
 
73
73
  Your file extensions tell the asset pipeline how to process the file. Use `.hbs` to compile the template with Handlebars.
74
74
 
75
- If your file is `templates/contacts/new.hbs`, the asset pipeline will generate Javascript code
75
+ If your file is `templates/contacts/new.hbs`, the asset pipeline will generate JavaScript code
76
76
 
77
- 1. Compile the Handlebars template to Javascript code
77
+ 1. Compile the Handlebars template to JavaScript code
78
78
  1. Add the template code to the `HandlebarsTemplates` global under the name `contacts/new`
79
79
 
80
- You can then invoke the resulting template in your application's Javascript
80
+ You can then invoke the resulting template in your application's JavaScript
81
81
 
82
82
  HandlebarsTemplates['contacts/new'](context);
83
83
 
84
84
  ## The template namespace
85
85
 
86
- By default, the global Javascript object that holds the compiled templates is `HandlebarsTemplates`, but it can
86
+ By default, the global JavaScript object that holds the compiled templates is `HandlebarsTemplates`, but it can
87
87
  be easily renamed. Another common template namespace is `JST`. Just change the `template_namespace` configuration option
88
88
  when you initialize your application.
89
89
 
90
90
  HandlebarsAssets::Config.template_namespace = 'JST'
91
91
 
92
+ ## `.hamlbars`
93
+
94
+ If you name your templates with the extension `.hamlbars`, you can use Haml syntax for your markup! Use `HandlebarsAssets::Config.haml_options` to pass custom options to the Haml rendering engine.
95
+
96
+ For example, if you have a file `widget.hamlbars` that looks like this:
97
+
98
+ %h1 {{title}}
99
+ %p {{body}}
100
+
101
+ Haml will first convert it to:
102
+
103
+ <h1> {{title}} </h1>
104
+ <p> {{body}} </p>
105
+
106
+ And the Handlebars will turn it into a JavaScript template.
107
+
92
108
  ## Partials
93
109
 
94
110
  If you begin the name of the template with an underscore, it will be recognized as a partial. You can invoke partials inside a template using the Handlebars partial syntax:
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_runtime_dependency "tilt"
23
23
  s.add_runtime_dependency "sprockets", ">= 2.0.3"
24
24
  s.add_development_dependency "rake"
25
+ s.add_development_dependency "haml"
25
26
  end
@@ -5,7 +5,7 @@ module HandlebarsAssets
5
5
  module Config
6
6
  extend self
7
7
 
8
- attr_writer :compiler, :compiler_path, :known_helpers, :known_helpers_only, :options, :path_prefix, :template_namespace
8
+ attr_writer :compiler, :compiler_path, :haml_options, :known_helpers, :known_helpers_only, :options, :path_prefix, :template_namespace
9
9
 
10
10
  def compiler
11
11
  @compiler || 'handlebars.js'
@@ -15,6 +15,14 @@ module HandlebarsAssets
15
15
  @compiler_path || HandlebarsAssets.path
16
16
  end
17
17
 
18
+ def haml_available?
19
+ defined? ::Haml::Engine
20
+ end
21
+
22
+ def haml_options
23
+ @haml_options || {}
24
+ end
25
+
18
26
  def known_helpers
19
27
  @known_helpers || []
20
28
  end
@@ -4,6 +4,7 @@ module HandlebarsAssets
4
4
  next unless app.assets
5
5
  app.assets.register_engine('.hbs', TiltHandlebars)
6
6
  app.assets.register_engine('.handlebars', TiltHandlebars)
7
+ app.assets.register_engine('.hamlbars', TiltHandlebars) if HandlebarsAssets::Config.haml_available?
7
8
  end
8
9
  end
9
10
  end
@@ -2,6 +2,7 @@ require 'tilt'
2
2
 
3
3
  module HandlebarsAssets
4
4
  class TiltHandlebars < Tilt::Template
5
+
5
6
  def self.default_mime_type
6
7
  'application/javascript'
7
8
  end
@@ -9,7 +10,13 @@ module HandlebarsAssets
9
10
  def evaluate(scope, locals, &block)
10
11
  template_path = TemplatePath.new(scope)
11
12
 
12
- compiled_hbs = Handlebars.precompile(data, HandlebarsAssets::Config.options)
13
+ source = if template_path.is_haml?
14
+ Haml::Engine.new(data, HandlebarsAssets::Config.haml_options).render
15
+ else
16
+ data
17
+ end
18
+
19
+ compiled_hbs = Handlebars.precompile(source, HandlebarsAssets::Config.options)
13
20
 
14
21
  template_namespace = HandlebarsAssets::Config.template_namespace
15
22
 
@@ -30,15 +37,26 @@ module HandlebarsAssets
30
37
  end
31
38
  end
32
39
 
40
+ def initialize_engine
41
+ require_template_library 'haml'
42
+ rescue LoadError
43
+ # haml not available
44
+ end
45
+
33
46
  protected
34
47
 
35
48
  def prepare; end
36
49
 
37
50
  class TemplatePath
38
51
  def initialize(scope)
52
+ self.full_path = scope.pathname
39
53
  self.template_path = scope.logical_path
40
54
  end
41
55
 
56
+ def is_haml?
57
+ full_path.to_s.end_with?('.hamlbars')
58
+ end
59
+
42
60
  def is_partial?
43
61
  template_path.gsub(%r{.*/}, '').start_with?('_')
44
62
  end
@@ -49,7 +67,7 @@ module HandlebarsAssets
49
67
 
50
68
  private
51
69
 
52
- attr_accessor :template_path
70
+ attr_accessor :full_path, :template_path
53
71
 
54
72
  def forced_underscore_name
55
73
  '_' + relative_path
@@ -1,3 +1,3 @@
1
1
  module HandlebarsAssets
2
- VERSION = "0.6.7"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ module HandlebarsAssets
4
+ class TiltHandlebarsTest < Test::Unit::TestCase
5
+ include SprocketsScope
6
+
7
+ def expected_haml_compiled(source)
8
+ Haml::Engine.new(source, HandlebarsAssets::Config.haml_options).render
9
+ end
10
+
11
+ def expected_hbs_compiled(source)
12
+ compiler_src = Pathname(HandlebarsAssets::Config.compiler_path).join(HandlebarsAssets::Config.compiler).read
13
+ ExecJS.compile(compiler_src).call('Handlebars.precompile', source, HandlebarsAssets::Config.options)
14
+ end
15
+
16
+ def haml_compiled(template_name, source)
17
+ compiled_haml = expected_haml_compiled(source)
18
+ compiled_hbs = expected_hbs_compiled(compiled_haml)
19
+ template_namespace = HandlebarsAssets::Config.template_namespace
20
+
21
+ <<END_EXPECTED
22
+ (function() {
23
+ this.#{template_namespace} || (this.#{template_namespace} = {});
24
+ this.#{template_namespace}[#{template_name.dump}] = Handlebars.template(#{compiled_hbs});
25
+ return this.#{template_namespace}[#{template_name.dump}];
26
+ }).call(this);
27
+ END_EXPECTED
28
+ end
29
+
30
+ def teardown
31
+ HandlebarsAssets::Config.reset!
32
+ HandlebarsAssets::Handlebars.reset!
33
+ end
34
+
35
+ def test_render_haml
36
+ root = '/myapp/app/assets/templates'
37
+ file = 'test_render.hamlbars'
38
+ scope = make_scope root, file
39
+ source = "%p This is {{handlebars}}"
40
+
41
+ template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
42
+
43
+ assert_equal haml_compiled('test_render', source), template.render(scope, {})
44
+ end
45
+ end
46
+ end
data/test/test_helper.rb CHANGED
@@ -25,6 +25,7 @@ module HandlebarsAssets
25
25
  def reset!
26
26
  @compiler = nil
27
27
  @compiler_path = nil
28
+ @haml_options = nil
28
29
  @known_helpers = nil
29
30
  @known_helpers_only = nil
30
31
  @options = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.7.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-11-12 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: execjs
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: haml
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: Compile Handlebars templates in the Rails asset pipeline.
79
95
  email:
80
96
  - leshill@gmail.com
@@ -98,6 +114,7 @@ files:
98
114
  - lib/handlebars_assets/tilt_handlebars.rb
99
115
  - lib/handlebars_assets/version.rb
100
116
  - test/edge/handlebars.js
117
+ - test/handlebars_assets/hamlbars_test.rb
101
118
  - test/handlebars_assets/tilt_edge_test.rb
102
119
  - test/handlebars_assets/tilt_handlebars_test.rb
103
120
  - test/test_helper.rb
@@ -129,6 +146,7 @@ specification_version: 3
129
146
  summary: Compile Handlebars templates in the Rails asset pipeline.
130
147
  test_files:
131
148
  - test/edge/handlebars.js
149
+ - test/handlebars_assets/hamlbars_test.rb
132
150
  - test/handlebars_assets/tilt_edge_test.rb
133
151
  - test/handlebars_assets/tilt_handlebars_test.rb
134
152
  - test/test_helper.rb