mustache_render 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +23 -0
- data/lib/mustache_render/core_ext/base_controller_ext.rb +2 -2
- data/lib/mustache_render/mustache/data.rb +99 -0
- data/lib/mustache_render/mustache.rb +4 -54
- data/lib/mustache_render/version.rb +1 -1
- data/spec/lib/mustache/basic_render_spec.rb +20 -0
- data/spec/lib/mustache/data_spec.rb +69 -0
- data/spec/lib/resources/templates/basic/8.mustache +1 -0
- metadata +52 -65
- data/README.rdoc +0 -22
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# MustacheRender
|
2
|
+
|
3
|
+
This project rocks and uses MIT-LICENSE.
|
4
|
+
|
5
|
+
## Rails 3.1.x
|
6
|
+
|
7
|
+
routes
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
Raily::Application.routes.draw do
|
11
|
+
::MustacheRender.adapter.manage_route_adapter self
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
install:
|
16
|
+
|
17
|
+
``` console
|
18
|
+
rails g mustache_render:migration:install
|
19
|
+
```
|
20
|
+
|
21
|
+
## 使用方法
|
22
|
+
|
23
|
+
|
@@ -16,7 +16,7 @@ module MustacheRender::CoreExt
|
|
16
16
|
impl_mustache_result_render result
|
17
17
|
end
|
18
18
|
|
19
|
-
def mustache_file_render template_path
|
19
|
+
def mustache_file_render template_path, mustache={}
|
20
20
|
result = ::MustacheRender::Mustache.file_render(template_path, mustache)
|
21
21
|
impl_mustache_result_render result
|
22
22
|
end
|
@@ -25,7 +25,7 @@ module MustacheRender::CoreExt
|
|
25
25
|
# 使用数据库中的模板进行渲染
|
26
26
|
# - template_path: 模板的路径
|
27
27
|
#
|
28
|
-
def mustache_db_render(template_path
|
28
|
+
def mustache_db_render(template_path, mustache={})
|
29
29
|
result = ::MustacheRender::Mustache.db_render(template_path, mustache)
|
30
30
|
impl_mustache_result_render result
|
31
31
|
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module MustacheRender
|
3
|
+
class Mustache
|
4
|
+
# 将所有的key转为字符串
|
5
|
+
class Data < ::Hash
|
6
|
+
def initialize options = {}
|
7
|
+
self.merge! options
|
8
|
+
end
|
9
|
+
|
10
|
+
def render(template='')
|
11
|
+
::MustacheRender::Mustache.render template, self
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_render path
|
15
|
+
::MustacheRender::Mustache.file_render path, self
|
16
|
+
end
|
17
|
+
|
18
|
+
def db_render path
|
19
|
+
::MustacheRender::Mustache.db_render path, self
|
20
|
+
end
|
21
|
+
|
22
|
+
def []=(name, value)
|
23
|
+
result = super(name.to_s, value)
|
24
|
+
self.each_pair do |k, v|
|
25
|
+
impl_stringify_keys v
|
26
|
+
end
|
27
|
+
# self.stringify_keys!
|
28
|
+
result
|
29
|
+
end
|
30
|
+
|
31
|
+
def [](name)
|
32
|
+
super(name.to_s)
|
33
|
+
end
|
34
|
+
|
35
|
+
# 深层复制
|
36
|
+
def deep_dup
|
37
|
+
duplicate = self.dup
|
38
|
+
duplicate.each_pair do |k, v|
|
39
|
+
tv = duplicate[k]
|
40
|
+
duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
|
41
|
+
end
|
42
|
+
duplicate
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# data = MustacheRender::Mustache::Data.new
|
47
|
+
# data.site = {
|
48
|
+
# :name => 'abc'
|
49
|
+
# }
|
50
|
+
#
|
51
|
+
# 等价于
|
52
|
+
#
|
53
|
+
# data['site'] = {
|
54
|
+
# :name => 'abc'
|
55
|
+
# }
|
56
|
+
#
|
57
|
+
def method_missing method_name, *args, &block
|
58
|
+
# 如果方法是赋值, 则直接赋值
|
59
|
+
if method_name.to_s[-1,1] == "="
|
60
|
+
self[method_name.to_s.chop!] = args.first
|
61
|
+
else
|
62
|
+
result = self[method_name.to_s]
|
63
|
+
|
64
|
+
if result.is_a?(Hash)
|
65
|
+
self[method_name.to_s] = Data.new(result)
|
66
|
+
elsif result.nil? # 如果为空的话,则生成一个新的对象
|
67
|
+
self[method_name.to_s] = Data.new {}
|
68
|
+
else
|
69
|
+
result
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def stringify_keys!
|
75
|
+
raise 'not impl'
|
76
|
+
end
|
77
|
+
|
78
|
+
def symbolize_keys!
|
79
|
+
raise 'not impl'
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def impl_stringify_keys(v)
|
85
|
+
if v.is_a?(::Hash)
|
86
|
+
v.keys.each do |key|
|
87
|
+
v[key.to_s] = v.delete(key) unless key.is_a?(::String)
|
88
|
+
end
|
89
|
+
elsif v.is_a?(::Array)
|
90
|
+
v.each do |_v|
|
91
|
+
impl_stringify_keys _v
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
require 'mustache_render/mustache/template'
|
3
3
|
require 'mustache_render/mustache/context'
|
4
|
+
require 'mustache_render/mustache/data'
|
4
5
|
|
5
6
|
module MustacheRender
|
6
7
|
class Mustache
|
@@ -24,14 +25,14 @@ module MustacheRender
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
|
-
#
|
28
|
+
# TODO: 语法检查,片段树
|
28
29
|
def partials_tree
|
29
30
|
|
30
31
|
end
|
31
32
|
|
32
33
|
# Context accessors.
|
33
34
|
#
|
34
|
-
# view = Mustache.new
|
35
|
+
# view = ::MustacheRender::Mustache.new
|
35
36
|
# view[:name] = "Jon"
|
36
37
|
# view.template = "Hi, {{name}}!"
|
37
38
|
# view.render # => "Hi, Jon!"
|
@@ -147,7 +148,6 @@ module MustacheRender
|
|
147
148
|
def partial(name)
|
148
149
|
name = self.class.generate_template_name name, config.file_template_extension
|
149
150
|
|
150
|
-
# return self.read_template_from_media name, media
|
151
151
|
@_cached_partials ||= {}
|
152
152
|
(@_cached_partials[media] ||= {})[name] ||= self.read_template_from_media name, media
|
153
153
|
end
|
@@ -194,56 +194,6 @@ module MustacheRender
|
|
194
194
|
CGI.escapeHTML(str)
|
195
195
|
end
|
196
196
|
|
197
|
-
#
|
198
|
-
# Private API
|
199
|
-
#
|
200
|
-
|
201
|
-
# When given a symbol or string representing a class, will try to produce an
|
202
|
-
# appropriate view class.
|
203
|
-
# e.g.
|
204
|
-
# Mustache.view_namespace = Hurl::Views
|
205
|
-
# Mustache.view_class(:Partial) # => Hurl::Views::Partial
|
206
|
-
def self.view_class(name)
|
207
|
-
if name != classify(name.to_s)
|
208
|
-
name = classify(name.to_s)
|
209
|
-
end
|
210
|
-
|
211
|
-
# Emptiness begets emptiness.
|
212
|
-
if name.to_s == ''
|
213
|
-
return Mustache
|
214
|
-
end
|
215
|
-
|
216
|
-
file_name = underscore(name)
|
217
|
-
|
218
|
-
name = "#{view_namespace}::#{name}"
|
219
|
-
|
220
|
-
if const = const_get!(name)
|
221
|
-
const
|
222
|
-
elsif File.exists?(file = "#{view_path}/#{file_name}.rb")
|
223
|
-
require "#{file}".chomp('.rb')
|
224
|
-
const_get!(name) || Mustache
|
225
|
-
else
|
226
|
-
Mustache
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
# Supercharged version of Module#const_get.
|
231
|
-
#
|
232
|
-
# Always searches under Object and can find constants by their full name,
|
233
|
-
# e.g. Mustache::Views::Index
|
234
|
-
#
|
235
|
-
# name - The full constant name to find.
|
236
|
-
#
|
237
|
-
# Returns the constant if found
|
238
|
-
# Returns nil if nothing is found
|
239
|
-
def self.const_get!(name)
|
240
|
-
name.split('::').inject(Object) do |klass, name|
|
241
|
-
klass.const_get(name)
|
242
|
-
end
|
243
|
-
rescue NameError
|
244
|
-
nil
|
245
|
-
end
|
246
|
-
|
247
197
|
# Has this template already been compiled? Compilation is somewhat
|
248
198
|
# expensive so it may be useful to check this before attempting it.
|
249
199
|
def self.compiled?
|
@@ -275,7 +225,7 @@ module MustacheRender
|
|
275
225
|
end.join('::')
|
276
226
|
end
|
277
227
|
|
278
|
-
#
|
228
|
+
# TemplatePartial => template_partial
|
279
229
|
# Template::Partial => template/partial
|
280
230
|
# Takes a string but defaults to using the current class' name.
|
281
231
|
def self.underscore(classified = name)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'MustacheRender Render Basic Render' do
|
5
|
+
context 'basic render' do
|
6
|
+
before :each do
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'basic render' do
|
11
|
+
viewer = ::MustacheRender::Mustache.new
|
12
|
+
|
13
|
+
viewer[:name] = 'Happy Wang'
|
14
|
+
viewer.template = '{{name}}'
|
15
|
+
viewer.render.should == 'Happy Wang'
|
16
|
+
viewer.file_render('basic/8').should == "in-8file:Happy Wang\n"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'MustacheRender Mustache Data' do
|
5
|
+
context 'basic test' do
|
6
|
+
let :data do
|
7
|
+
MustacheRender::Mustache::Data.new
|
8
|
+
end
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'render basic' do
|
15
|
+
data[:a] = 'hello'
|
16
|
+
data.b = 'happy'
|
17
|
+
data.render('{{a}}{{b}}').should == 'hellohappy'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'render a array' do
|
21
|
+
data.a = [{:a => 1, :b => 2}, {:a => 10, :b => 20}]
|
22
|
+
data.render('{{#a}}a:{{a}} b:{{b}}{{/a}}').should == "a:1 b:2a:10 b:20"
|
23
|
+
end
|
24
|
+
|
25
|
+
it '所有的key转为字符串' do
|
26
|
+
data[:a] = 'b'
|
27
|
+
data.inspect.should == "{\"a\"=>\"b\"}"
|
28
|
+
end
|
29
|
+
|
30
|
+
it '当为数组的时候,每个元素也许要进行key字符串化' do
|
31
|
+
data.b.c = [{:a => 'a', 'b' => 'b'}]
|
32
|
+
data.inspect.should == "{\"b\"=>{\"c\"=>[{\"b\"=>\"b\", \"a\"=>\"a\"}]}}"
|
33
|
+
end
|
34
|
+
|
35
|
+
it '基本赋值方法' do
|
36
|
+
|
37
|
+
data.site = {
|
38
|
+
:name => 'a'
|
39
|
+
}
|
40
|
+
|
41
|
+
data.inspect.should == "{\"site\"=>{\"name\"=>\"a\"}}"
|
42
|
+
data.site.inspect.should == "{\"name\"=>\"a\"}"
|
43
|
+
end
|
44
|
+
|
45
|
+
it '取值类型为数组' do
|
46
|
+
data.sites = [1, 2, 3]
|
47
|
+
|
48
|
+
data.sites.inspect.should == '[1, 2, 3]'
|
49
|
+
end
|
50
|
+
|
51
|
+
it '直接调用一个空方法' do
|
52
|
+
data.helloworld_abcd.should == {}
|
53
|
+
data.helloworld_abcd.class.should == ::MustacheRender::Mustache::Data
|
54
|
+
end
|
55
|
+
|
56
|
+
it '连续的调用一个方法' do
|
57
|
+
data.a.b.c.should == {}
|
58
|
+
|
59
|
+
data.a.b.c.sites = [1, 2, 3]
|
60
|
+
data.a.b.c.sites.inspect.should == '[1, 2, 3]'
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'hello? 这类方法的生成是否正常' do
|
64
|
+
data.a['hello?'] = false
|
65
|
+
data.a.hello?.should == false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
in-8file:{{name}}
|
metadata
CHANGED
@@ -1,61 +1,55 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mustache_render
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 12
|
10
|
-
version: 0.0.12
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.13
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- happy
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: awesome_nested_set
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
33
22
|
type: :runtime
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: sqlite3
|
37
23
|
prerelease: false
|
38
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
25
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
47
38
|
type: :development
|
48
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
49
46
|
description: Description of MustacheRender.
|
50
|
-
email:
|
47
|
+
email:
|
51
48
|
- andywang7259@gmail.com
|
52
49
|
executables: []
|
53
|
-
|
54
50
|
extensions: []
|
55
|
-
|
56
51
|
extra_rdoc_files: []
|
57
|
-
|
58
|
-
files:
|
52
|
+
files:
|
59
53
|
- lib/mustache_render.rb
|
60
54
|
- lib/tasks/mustache_render_tasks.rake
|
61
55
|
- lib/generators/mustache_render/install_generator.rb
|
@@ -69,6 +63,7 @@ files:
|
|
69
63
|
- lib/mustache_render/mustache/context.rb
|
70
64
|
- lib/mustache_render/mustache/template.rb
|
71
65
|
- lib/mustache_render/mustache/parser.rb
|
66
|
+
- lib/mustache_render/mustache/data.rb
|
72
67
|
- lib/mustache_render/controllers/mustache_render/manage/base_controller.rb
|
73
68
|
- lib/mustache_render/controllers/mustache_render/manage/templates_controller.rb
|
74
69
|
- lib/mustache_render/controllers/mustache_render/manage/template_versions_controller.rb
|
@@ -111,12 +106,15 @@ files:
|
|
111
106
|
- lib/mustache_render/views/mustache_render/manage/folders/_new_2.html.erb
|
112
107
|
- lib/mustache_render/views/mustache_render/manage/folders/_edit_2.html.erb
|
113
108
|
- lib/mustache_render/adapter.rb
|
109
|
+
- spec/lib/mustache/data_spec.rb
|
114
110
|
- spec/lib/mustache/file_render_spec.rb
|
111
|
+
- spec/lib/mustache/basic_render_spec.rb
|
115
112
|
- spec/lib/resources/templates/basic/6.mustache
|
116
113
|
- spec/lib/resources/templates/basic/2.mustache
|
117
114
|
- spec/lib/resources/templates/basic/1.mustache
|
118
115
|
- spec/lib/resources/templates/basic/7.mustache
|
119
116
|
- spec/lib/resources/templates/basic/3.mustache
|
117
|
+
- spec/lib/resources/templates/basic/8.mustache
|
120
118
|
- spec/lib/resources/templates/basic/5.mustache
|
121
119
|
- spec/lib/resources/templates/basic/4.mustache
|
122
120
|
- spec/lib/resources/templates/scan_tags/2.mustache
|
@@ -125,40 +123,29 @@ files:
|
|
125
123
|
- spec/spec_helper.rb
|
126
124
|
- MIT-LICENSE
|
127
125
|
- Rakefile
|
128
|
-
- README.
|
129
|
-
has_rdoc: true
|
126
|
+
- README.md
|
130
127
|
homepage: http://blogsoso.net/
|
131
128
|
licenses: []
|
132
|
-
|
133
129
|
post_install_message:
|
134
130
|
rdoc_options: []
|
135
|
-
|
136
|
-
require_paths:
|
131
|
+
require_paths:
|
137
132
|
- lib
|
138
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
134
|
none: false
|
140
|
-
requirements:
|
141
|
-
- -
|
142
|
-
- !ruby/object:Gem::Version
|
143
|
-
|
144
|
-
|
145
|
-
- 0
|
146
|
-
version: "0"
|
147
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
140
|
none: false
|
149
|
-
requirements:
|
150
|
-
- -
|
151
|
-
- !ruby/object:Gem::Version
|
152
|
-
|
153
|
-
segments:
|
154
|
-
- 0
|
155
|
-
version: "0"
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
156
145
|
requirements: []
|
157
|
-
|
158
146
|
rubyforge_project:
|
159
|
-
rubygems_version: 1.
|
147
|
+
rubygems_version: 1.8.24
|
160
148
|
signing_key:
|
161
149
|
specification_version: 3
|
162
150
|
summary: Summary of MustacheRender.
|
163
151
|
test_files: []
|
164
|
-
|
data/README.rdoc
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
= MustacheRender
|
2
|
-
|
3
|
-
This project rocks and uses MIT-LICENSE.
|
4
|
-
|
5
|
-
= Rails 3.1.x
|
6
|
-
|
7
|
-
routes
|
8
|
-
|
9
|
-
```
|
10
|
-
namespace :mustache_render do
|
11
|
-
namespace :manager do
|
12
|
-
resources :folders do
|
13
|
-
resources :templates, :only => [:show, :new, :edit, :update, :create, :destroy]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
```
|
18
|
-
|
19
|
-
install:
|
20
|
-
|
21
|
-
rails g mustache_render:migration:install
|
22
|
-
|