haml_coffee_assets 1.9.1 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Haml Coffee Assets [](http://travis-ci.org/netzpirat/haml_coffee_assets)
|
2
2
|
|
3
|
-
Haml Coffee Assets compiles [Haml Coffee](https://github.com/netzpirat/haml-coffee) templates in the Rails 3.1
|
4
|
-
asset pipeline, so you can use them as JavaScript templates in your JavaScript heavy Rails application. It also works as
|
5
|
-
a pure [Sprockets](https://github.com/sstephenson/sprockets) engine without Rails.
|
3
|
+
Haml Coffee Assets compiles [Haml Coffee](https://github.com/netzpirat/haml-coffee) templates in the Rails 3.1 asset pipeline, so you can use them as JavaScript templates in your JavaScript heavy Rails application. Server-side rendering of templates is also possible, allowing you to share the same template files for Rails and JavaScript templates. It also works as a pure [Sprockets](https://github.com/sstephenson/sprockets) engine without Rails.
|
6
4
|
|
7
5
|
Tested on MRI Ruby 1.8.7, 1.9.2, 1.9.3, REE and the latest version of JRuby.
|
8
6
|
|
@@ -12,6 +10,7 @@ Tested on MRI Ruby 1.8.7, 1.9.2, 1.9.3, REE and the latest version of JRuby.
|
|
12
10
|
standalone Sprockets engine.
|
13
11
|
* Manifold options to configure Haml Coffee Assets to your needs.
|
14
12
|
* AMD support.
|
13
|
+
* Server-side rendering of templates in Rails.
|
15
14
|
|
16
15
|
## Haml Coffee
|
17
16
|
|
@@ -107,6 +106,36 @@ all templates from your `app/assets/javascripts/application.js.coffee`:
|
|
107
106
|
If you would place your templates into `app/assets/javascripts/templates`, then all your JST template names would begin
|
108
107
|
with `templates/`, which may be not what you want.
|
109
108
|
|
109
|
+
### Server-side rendering in Rails
|
110
|
+
|
111
|
+
Haml Coffee Assets registers the `.hamlc` extension with Action View, so that Rails templates can be written in Haml Coffee. Rails will see templates placed in `app/assets/javascripts/templates` (though this path can be changed if you store your templates in another directory), and the same template files can be rendered via Rails or via JavaScript on the client.
|
112
|
+
|
113
|
+
Given a Haml Coffee template at `app/assets/javascripts/templates/books/_book.hamlc`:
|
114
|
+
|
115
|
+
```haml
|
116
|
+
%dl
|
117
|
+
%dt Name
|
118
|
+
%dd= @name
|
119
|
+
%dt Author
|
120
|
+
%dd= @author
|
121
|
+
```
|
122
|
+
|
123
|
+
Rendering `books#index`:
|
124
|
+
|
125
|
+
```haml
|
126
|
+
= render "book", :name => "A Tale of Two Cities", :author => "Charles Dickens"
|
127
|
+
```
|
128
|
+
|
129
|
+
Require and render the same file on the client using the asset pipeline:
|
130
|
+
|
131
|
+
```coffeescript
|
132
|
+
#= require templates/books/_book
|
133
|
+
|
134
|
+
JST["books/book"](name: "A Tale of Two Cities", author: "Charles Dickens")
|
135
|
+
```
|
136
|
+
|
137
|
+
Note that the template is required as `books/_book` because it refers to the actual file, but the template name on the client is simply `books/book`. If you require all templates at once with `#= require_tree ./templates`, you won't need to remember this distinction.
|
138
|
+
|
110
139
|
## Configuration
|
111
140
|
|
112
141
|
_Please note that all configuration examples will use the paths of the Haml Coffee template generation and not the
|
@@ -425,6 +454,14 @@ You can see the [default implementation](https://github.com/netzpirat/haml_coffe
|
|
425
454
|
and the [Haml Coffee documentation](https://github.com/netzpirat/haml-coffee#custom-helper-function-options)
|
426
455
|
for more information about each helper function.
|
427
456
|
|
457
|
+
### Shared template path
|
458
|
+
|
459
|
+
Rails will look for templates in `app/assets/javascripts/templates` when rendering on the server side. If you store your templates in another directory, you can change this location:
|
460
|
+
|
461
|
+
```ruby
|
462
|
+
config.hamlcoffee.shared_template_path = "custom/template/path"
|
463
|
+
```
|
464
|
+
|
428
465
|
## Partial rendering
|
429
466
|
|
430
467
|
With Haml Coffee Assets you can render partials when using plain JST approach and also with AMD support.
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module HamlCoffeeAssets
|
2
|
+
module ActionView
|
3
|
+
class TemplateHandler
|
4
|
+
DEPENDENCY_PATTERN = /(?:window\.)?JST(?:\[["']([\w\/]+)["']\]|\.(\w+))/
|
5
|
+
|
6
|
+
def self.call(template)
|
7
|
+
new(template).render
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(template, partial = false, dependencies = [])
|
11
|
+
@template = template
|
12
|
+
@partial = partial
|
13
|
+
@dependencies = dependencies
|
14
|
+
end
|
15
|
+
|
16
|
+
def render
|
17
|
+
"ExecJS.compile(#{ compilation_string }).eval(#{ evaluation_string }).html_safe"
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def compilation_string
|
23
|
+
string = ''
|
24
|
+
|
25
|
+
unless @partial
|
26
|
+
string << preamble
|
27
|
+
string << helpers
|
28
|
+
end
|
29
|
+
|
30
|
+
string << compiled_template
|
31
|
+
|
32
|
+
if @partial
|
33
|
+
string
|
34
|
+
else
|
35
|
+
string.inspect
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def evaluation_string
|
42
|
+
string = "JST['#{ logical_path }'](\#{local_assigns.to_json})"
|
43
|
+
string.inspect.sub(/\\#/, '#')
|
44
|
+
end
|
45
|
+
|
46
|
+
def preamble
|
47
|
+
"var window = { JST: {} }, JST = window.JST;\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
def helpers
|
51
|
+
::HamlCoffeeAssets.helpers
|
52
|
+
end
|
53
|
+
|
54
|
+
def compiled_template
|
55
|
+
compiled = ::HamlCoffeeAssets::Compiler.compile(
|
56
|
+
logical_path,
|
57
|
+
@template.source
|
58
|
+
)
|
59
|
+
|
60
|
+
include_dependencies(compiled)
|
61
|
+
end
|
62
|
+
|
63
|
+
def include_dependencies(compiled)
|
64
|
+
compiled.dup.scan(DEPENDENCY_PATTERN) do |match|
|
65
|
+
match.compact!
|
66
|
+
|
67
|
+
path = match[0]
|
68
|
+
|
69
|
+
if path == logical_path || @dependencies.include?(path)
|
70
|
+
next
|
71
|
+
else
|
72
|
+
@dependencies << path
|
73
|
+
end
|
74
|
+
|
75
|
+
partial = ::ActionView::Template.new(
|
76
|
+
partial_source(path),
|
77
|
+
path,
|
78
|
+
self.class,
|
79
|
+
:virtual_path => partial_path(path)
|
80
|
+
)
|
81
|
+
|
82
|
+
compiled << self.class.new(
|
83
|
+
partial,
|
84
|
+
true,
|
85
|
+
@dependencies
|
86
|
+
).compilation_string
|
87
|
+
end
|
88
|
+
|
89
|
+
compiled
|
90
|
+
end
|
91
|
+
|
92
|
+
def logical_path
|
93
|
+
return @logical_path if defined?(@logical_path)
|
94
|
+
|
95
|
+
path = @template.virtual_path.split('/')
|
96
|
+
path.last.sub!(/^_/, '')
|
97
|
+
@logical_path = path.join('/')
|
98
|
+
end
|
99
|
+
|
100
|
+
def partial_source(path)
|
101
|
+
::Rails.root.join(
|
102
|
+
::HamlCoffeeAssets.config.shared_template_path,
|
103
|
+
partial_path(path) + '.hamlc'
|
104
|
+
).read
|
105
|
+
end
|
106
|
+
|
107
|
+
def partial_path(path)
|
108
|
+
parts = path.split('/')
|
109
|
+
parts[-1] = "_#{ parts[-1] }"
|
110
|
+
parts.join('/')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -38,7 +38,12 @@ module HamlCoffeeAssets
|
|
38
38
|
self.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base'
|
39
39
|
self.context = 'window.HAML.context'
|
40
40
|
self.extendScope = false
|
41
|
-
self.
|
41
|
+
self.shared_template_path = 'app/assets/javascripts/templates'
|
42
|
+
self.name_filter = lambda { |n|
|
43
|
+
parts = n.sub(/^templates\//, '').split('/')
|
44
|
+
parts.last.sub!(/^_/, '')
|
45
|
+
parts.join('/')
|
46
|
+
}
|
42
47
|
end
|
43
48
|
|
44
49
|
# Template namespace
|
@@ -131,6 +136,10 @@ module HamlCoffeeAssets
|
|
131
136
|
#
|
132
137
|
attr_accessor :name_filter
|
133
138
|
|
139
|
+
# Path to templates shared by Rails and JS.
|
140
|
+
#
|
141
|
+
attr_accessor :shared_template_path
|
142
|
+
|
134
143
|
end
|
135
144
|
|
136
145
|
end
|
@@ -10,12 +10,24 @@ module HamlCoffeeAssets
|
|
10
10
|
|
11
11
|
config.hamlcoffee = ::HamlCoffeeAssets.config
|
12
12
|
|
13
|
+
# Add shared template path to ActionView's load path
|
14
|
+
config.before_configuration do |app|
|
15
|
+
app.paths['app/views'] << config.hamlcoffee.shared_template_path
|
16
|
+
end
|
17
|
+
|
13
18
|
# Initialize Haml Coffee Assets after Sprockets
|
14
19
|
#
|
15
20
|
initializer 'sprockets.hamlcoffeeassets', :group => :all, :after => 'sprockets.environment' do |app|
|
21
|
+
require 'haml_coffee_assets/action_view/template_handler'
|
22
|
+
|
23
|
+
# Register Tilt template (for ActionView)
|
24
|
+
ActiveSupport.on_load(:action_view) do
|
25
|
+
::ActionView::Template.register_template_handler(:hamlc, ::HamlCoffeeAssets::ActionView::TemplateHandler)
|
26
|
+
end
|
27
|
+
|
16
28
|
next unless app.assets
|
17
29
|
|
18
|
-
# Register
|
30
|
+
# Register Tilt template (for Sprockets)
|
19
31
|
app.assets.register_engine '.hamlc', ::HamlCoffeeAssets::Tilt::TemplateHandler
|
20
32
|
end
|
21
33
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: haml_coffee_assets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.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: 2013-01-
|
12
|
+
date: 2013-01-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: coffee-script
|
@@ -82,6 +82,7 @@ executables: []
|
|
82
82
|
extensions: []
|
83
83
|
extra_rdoc_files: []
|
84
84
|
files:
|
85
|
+
- lib/haml_coffee_assets/action_view/template_handler.rb
|
85
86
|
- lib/haml_coffee_assets/compiler.rb
|
86
87
|
- lib/haml_coffee_assets/configuration.rb
|
87
88
|
- lib/haml_coffee_assets/rails/engine.rb
|