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.
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +24 -10
- data/lib/hogan_assets/config.rb +13 -8
- data/lib/hogan_assets/tilt.rb +30 -3
- data/lib/hogan_assets/version.rb +1 -1
- data/test/hogan_assets/tilt_test.rb +47 -17
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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`.
|
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
|
-
###
|
62
|
+
### Lambda Support
|
65
63
|
|
66
|
-
**
|
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.
|
67
|
+
config.lambda_support = true
|
70
68
|
end
|
71
69
|
|
72
|
-
###
|
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
|
-
|
74
|
+
HoganTemplates['app/templates/posts/index']
|
75
75
|
|
76
|
-
|
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.
|
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
|
data/lib/hogan_assets/config.rb
CHANGED
@@ -6,23 +6,32 @@ module HoganAssets
|
|
6
6
|
# Or in a block:
|
7
7
|
#
|
8
8
|
# HoganAssets::Config.configure do |config|
|
9
|
-
# config.
|
10
|
-
# config.
|
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
|
-
|
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
|
data/lib/hogan_assets/tilt.rb
CHANGED
@@ -11,8 +11,10 @@ module HoganAssets
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def evaluate(scope, locals, &block)
|
14
|
-
|
15
|
-
|
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[#{
|
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
|
data/lib/hogan_assets/version.rb
CHANGED
@@ -2,16 +2,22 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module HoganAssets
|
4
4
|
class TiltTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
+
define_method(:pathname) { Pathname.new(root) + file }
|
10
11
|
|
11
|
-
|
12
|
+
define_method(:root_path) { root }
|
13
|
+
|
14
|
+
define_method(:s_path) { pathname.to_s }
|
12
15
|
end.new
|
13
|
-
|
14
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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.
|
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-
|
12
|
+
date: 2012-08-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: execjs
|