templates_for 0.0.1
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/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +58 -0
- data/Rakefile +9 -0
- data/lib/templates_for.rb +11 -0
- data/lib/templates_for/action_controller.rb +22 -0
- data/lib/templates_for/action_view.rb +23 -0
- data/lib/templates_for/railtie.rb +13 -0
- data/lib/templates_for/version.rb +3 -0
- data/spec/fake_app.rb +20 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/templates/test_one/_item.html.erb +1 -0
- data/spec/templates/test_one/_nav.html.erb +3 -0
- data/spec/templates_for/action_controller_spec.rb +21 -0
- data/spec/templates_for/action_view_spec.rb +30 -0
- data/spec/views/test_one/index.html.erb +2 -0
- data/templates_for.gemspec +26 -0
- metadata +131 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
## Templates For
|
2
|
+
|
3
|
+
If we have a view with a Javascript application on it our directory
|
4
|
+
structure would look like this:
|
5
|
+
|
6
|
+
app/templates/widgets/_nav.html.erb
|
7
|
+
app/templates/widgets/_details.html.erb
|
8
|
+
|
9
|
+
app/views/widgets/show.html.erb
|
10
|
+
|
11
|
+
In the details template we might have:
|
12
|
+
|
13
|
+
```html
|
14
|
+
<div class="details">
|
15
|
+
<p>{{name}}</p>
|
16
|
+
</nav>
|
17
|
+
```
|
18
|
+
|
19
|
+
### Gemfile
|
20
|
+
|
21
|
+
gem 'sorted', '~> 0.0.1'
|
22
|
+
|
23
|
+
### Controller
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
WidgetController < ActionController::Base
|
27
|
+
|
28
|
+
templates_for :show, %( nav details )
|
29
|
+
|
30
|
+
def show
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
### View
|
37
|
+
|
38
|
+
```erb
|
39
|
+
<%= templates_for_view %>
|
40
|
+
```
|
41
|
+
|
42
|
+
### Javascript
|
43
|
+
|
44
|
+
```javascript
|
45
|
+
var nav = JST['nav'];
|
46
|
+
var details = JST['details'];
|
47
|
+
|
48
|
+
el = document.getElementById('my_elem');
|
49
|
+
el.innerHTML = Mustache.render(details, {name: "Bob"});
|
50
|
+
```
|
51
|
+
|
52
|
+
### Other libs
|
53
|
+
|
54
|
+
eco (recommended): https://github.com/sstephenson/eco
|
55
|
+
|
56
|
+
underscore.js: http://documentcloud.github.com/underscore/#template
|
57
|
+
|
58
|
+
mustach.js: https://github.com/janl/mustache.js
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module TemplatesFor
|
2
|
+
module ActionController
|
3
|
+
module ClassMethods
|
4
|
+
def templates_for(action, templates)
|
5
|
+
action_templates[action.to_sym] = templates
|
6
|
+
end
|
7
|
+
|
8
|
+
def action_templates
|
9
|
+
@action_templates ||= {}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.send :extend, ClassMethods
|
15
|
+
base.append_view_path "app/templates"
|
16
|
+
end
|
17
|
+
|
18
|
+
def templates_for_action
|
19
|
+
self.class.action_templates[action_name.to_sym] || []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TemplatesFor
|
2
|
+
module ActionView
|
3
|
+
def templates_for_view
|
4
|
+
raw(render_templates + "\n" + parse_templates)
|
5
|
+
end
|
6
|
+
|
7
|
+
def render_templates
|
8
|
+
controller.templates_for_action.map do |template|
|
9
|
+
content_tag 'script', { :type => 'text/template', :id => "#{template}_template" }, false do
|
10
|
+
raw("\n" + render(template).gsub(/<!--(.*?)-->/, '').strip + "\n")
|
11
|
+
end
|
12
|
+
end.join("\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse_templates
|
16
|
+
content_tag 'script', { :type => 'text/javascript' }, false do
|
17
|
+
"\n var JST = {};\n" + controller.templates_for_action.map do |template|
|
18
|
+
" JST['#{template}'] = document.getElementById('#{template}_template').innerHTML;"
|
19
|
+
end.join("\n") + "\n"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module TemplatesFor
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer 'templates_for.configure' do |app|
|
4
|
+
ActiveSupport.on_load :action_view do
|
5
|
+
::ActionView::Base.send :include, ::TemplatesFor::ActionView
|
6
|
+
end
|
7
|
+
|
8
|
+
ActiveSupport.on_load :action_controller do
|
9
|
+
::ActionController::Base.send :include, ::TemplatesFor::ActionController
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/fake_app.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'action_controller/railtie'
|
2
|
+
require 'action_view/railtie'
|
3
|
+
|
4
|
+
app = Class.new(Rails::Application)
|
5
|
+
app.config.active_support.deprecation = :stdout
|
6
|
+
app.initialize!
|
7
|
+
|
8
|
+
class ApplicationController < ActionController::Base
|
9
|
+
append_view_path "spec/templates"
|
10
|
+
append_view_path "spec/views"
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestOneController < ApplicationController
|
14
|
+
templates_for :show, %w(nav)
|
15
|
+
templates_for :index, %w(nav item)
|
16
|
+
end
|
17
|
+
|
18
|
+
class TestTwoController < ApplicationController
|
19
|
+
templates_for :show, %w(menu)
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
<p>Item number: {{item}}<p>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'TemplatesFor::ActionController' do
|
4
|
+
it "test controller one should have a nav template for the show action and nothing else" do
|
5
|
+
controller = TestOneController.new
|
6
|
+
controller.action_name = 'show'
|
7
|
+
controller.templates_for_action.should eq ['nav']
|
8
|
+
end
|
9
|
+
|
10
|
+
it "test controller one should have a nav template for the index action and nothing else" do
|
11
|
+
controller = TestOneController.new
|
12
|
+
controller.action_name = 'index'
|
13
|
+
controller.templates_for_action.should eq ['nav', 'item']
|
14
|
+
end
|
15
|
+
|
16
|
+
it "test controller two should have a menu template for the show action and nothing else" do
|
17
|
+
controller = TestTwoController.new
|
18
|
+
controller.action_name = 'show'
|
19
|
+
controller.templates_for_action.should eq ['menu']
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'TemplatesFor::ActionView' do
|
4
|
+
before(:each) do
|
5
|
+
@controller = TestOneController.new
|
6
|
+
@controller.action_name = 'index'
|
7
|
+
@view = ActionView::Base.new(@controller.view_paths, [], @controller)
|
8
|
+
@view.controller = @controller
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should render the templates" do
|
12
|
+
template = @view.render :template => 'test_one/index'
|
13
|
+
template.should eq <<-EOL
|
14
|
+
<p>test</p>
|
15
|
+
<script id="nav_template" type="text/template">
|
16
|
+
<nav class="test">
|
17
|
+
<a href="#/{{url}}">link</a>
|
18
|
+
</nav>
|
19
|
+
</script>
|
20
|
+
<script id="item_template" type="text/template">
|
21
|
+
<p>Item number: {{item}}<p>
|
22
|
+
</script>
|
23
|
+
<script type="text/javascript">
|
24
|
+
var JST = {};
|
25
|
+
JST['nav'] = document.getElementById('nav_template').innerHTML;
|
26
|
+
JST['item'] = document.getElementById('item_template').innerHTML;
|
27
|
+
</script>
|
28
|
+
EOL
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "templates_for/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "templates_for"
|
7
|
+
s.version = TemplatesFor::VERSION
|
8
|
+
s.authors = ["Rufus Post"]
|
9
|
+
s.email = ["rufus@reinteractive.net"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Render js templates in a view}
|
12
|
+
s.description = %q{Generate js templates}
|
13
|
+
|
14
|
+
s.rubyforge_project = "templates_for"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'railties', '>= 3.0.0'
|
22
|
+
s.add_dependency 'actionpack', '>= 3.0.0'
|
23
|
+
s.add_dependency 'tzinfo', '>= 0.3.31'
|
24
|
+
s.add_development_dependency 'rspec'
|
25
|
+
s.add_development_dependency 'rake'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: templates_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rufus Post
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: &70300524170020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70300524170020
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: actionpack
|
27
|
+
requirement: &70300524169420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70300524169420
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: tzinfo
|
38
|
+
requirement: &70300524168900 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.3.31
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70300524168900
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &70300524168500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70300524168500
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rake
|
60
|
+
requirement: &70300524167960 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70300524167960
|
69
|
+
description: Generate js templates
|
70
|
+
email:
|
71
|
+
- rufus@reinteractive.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/templates_for.rb
|
82
|
+
- lib/templates_for/action_controller.rb
|
83
|
+
- lib/templates_for/action_view.rb
|
84
|
+
- lib/templates_for/railtie.rb
|
85
|
+
- lib/templates_for/version.rb
|
86
|
+
- spec/fake_app.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/templates/test_one/_item.html.erb
|
89
|
+
- spec/templates/test_one/_nav.html.erb
|
90
|
+
- spec/templates_for/action_controller_spec.rb
|
91
|
+
- spec/templates_for/action_view_spec.rb
|
92
|
+
- spec/views/test_one/index.html.erb
|
93
|
+
- templates_for.gemspec
|
94
|
+
homepage: ''
|
95
|
+
licenses: []
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
hash: 4216230895902618791
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
hash: 4216230895902618791
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project: templates_for
|
120
|
+
rubygems_version: 1.8.11
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: Render js templates in a view
|
124
|
+
test_files:
|
125
|
+
- spec/fake_app.rb
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/templates/test_one/_item.html.erb
|
128
|
+
- spec/templates/test_one/_nav.html.erb
|
129
|
+
- spec/templates_for/action_controller_spec.rb
|
130
|
+
- spec/templates_for/action_view_spec.rb
|
131
|
+
- spec/views/test_one/index.html.erb
|