mustache_render 0.0.3 → 0.0.4

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.
@@ -12,11 +12,47 @@ module MustacheRender
12
12
  def initialize
13
13
  end
14
14
 
15
+ #
16
+ # 默认的渲染媒介
17
+ #
18
+ def default_render_media
19
+ @default_render_media ||= :db
20
+ end
21
+
22
+ def default_render_media= media
23
+ @default_render_media ||= media
24
+ end
25
+
15
26
  #
16
27
  # lib 的基本路径
17
28
  #
18
29
  def lib_base_path
19
30
  File.dirname(__FILE__)
20
31
  end
32
+
33
+ def file_template_root_path
34
+ @file_template_root_path ||= "#{lib_base_path}/mustache_render/templates"
35
+ end
36
+
37
+ def file_template_root_path= path
38
+ @file_template_root_path ||= path
39
+ end
40
+
41
+ def file_template_extension
42
+ @file_template_extension ||= 'mustache'
43
+ end
44
+
45
+ def file_template_extension= name
46
+ @file_template_extension ||= name
47
+ end
48
+
49
+ def raise_on_context_miss?
50
+ defined?(@raise_on_context_miss) ? @raise_on_context_miss : false
51
+ end
52
+
53
+ def raise_on_context_miss=(boolean)
54
+ @raise_on_context_miss = boolean
55
+ end
56
+
21
57
  end
22
58
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module MustacheRender::Manager
2
3
  class BaseController < ::ApplicationController
3
4
  before_filter :set_mustache_manager_view_path
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module MustacheRender::Manager
2
3
  class FoldersController < BaseController
3
4
  def index
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module MustacheRender::Manager
2
3
  class TemplatesController < BaseController
3
4
  before_filter :load_folder_record
@@ -11,25 +11,31 @@ module MustacheRender::CoreExt
11
11
  end
12
12
 
13
13
  module ClassMethods
14
- # def acts_as_mustache_renderer
15
- # helper_method :mustache_render
16
- # include InstanceMethods
17
- # end
18
14
  end
19
15
 
20
16
  module InstanceMethods
21
- # def mustache_render(mustache_template='', mustache={})
22
- # render :text => ::Mustache.render(mustache_template, mustache)
23
- # end
17
+ def mustache_render template='', mustache={}
18
+ result = ::MustacheRender::Mustache.render(template, mustache)
19
+ impl_mustache_result_render result
20
+ end
21
+
22
+ def mustache_file_render template_path=nil, mustache={}
23
+ result = ::MustacheRender::Mustache.file_render(template_path, mustache)
24
+ impl_mustache_result_render result
25
+ end
24
26
 
25
27
  #
26
28
  # 使用数据库中的模板进行渲染
27
29
  # - template_path: 模板的路径
28
30
  #
29
31
  def mustache_db_render(template_path=nil, mustache={})
30
- # result = ::Mustache.render(template_path, mustache)
31
- result = ::MustacheRender::Mustache.render_file(template_path, mustache)
32
+ result = ::MustacheRender::Mustache.db_render(template_path, mustache)
33
+ impl_mustache_result_render result
34
+ end
35
+
36
+ private
32
37
 
38
+ def impl_mustache_result_render(result)
33
39
  if self.is_a?(ActionController::Base)
34
40
  render :text => result
35
41
  else
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module MustacheRender
2
3
  class Mustache
3
4
  # A ContextMiss is raised whenever a tag's target can not be found
@@ -103,7 +104,7 @@ module MustacheRender
103
104
  end
104
105
  end
105
106
 
106
- if default == :__raise || mustache_in_stack.raise_on_context_miss?
107
+ if default == :__raise || MustacheRender.config.raise_on_context_miss?
107
108
  raise ContextMiss.new("Can't find #{name} in #{@stack.inspect}")
108
109
  else
109
110
  default
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module MustacheRender
2
3
  class Mustache
3
4
  # The Generator is in charge of taking an array of Mustache tokens,
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'strscan'
2
3
 
3
4
  module MustacheRender
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'cgi'
2
3
 
3
4
  require 'mustache_render/mustache/parser'
@@ -1,122 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'mustache_render/mustache/template'
2
3
  require 'mustache_render/mustache/context'
3
- require 'mustache_render/mustache/settings'
4
-
5
- # Mustache is the base class from which your Mustache subclasses
6
- # should inherit (though it can be used on its own).
7
- #
8
- # The typical Mustache workflow is as follows:
9
- #
10
- # * Create a Mustache subclass: class Stats < Mustache
11
- # * Create a template: stats.mustache
12
- # * Instantiate an instance: view = Stats.new
13
- # * Render that instance: view.render
14
- #
15
- # You can skip the instantiation by calling `Stats.render` directly.
16
- #
17
- # While Mustache will do its best to load and render a template for
18
- # you, this process is completely customizable using a few options.
19
- #
20
- # All settings can be overriden at the class level.
21
- #
22
- # For example, going with the above example, we can use
23
- # `Stats.template_path = "/usr/local/templates"` to specify the path
24
- # Mustache uses to find templates.
25
- #
26
- # Here are the available options:
27
- #
28
- # * template_path
29
- #
30
- # The `template_path` setting determines the path Mustache uses when
31
- # looking for a template. By default it is "."
32
- # Setting it to /usr/local/templates, for example, means (given all
33
- # other settings are default) a Mustache subclass `Stats` will try to
34
- # load /usr/local/templates/stats.mustache
35
- #
36
- # * template_extension
37
- #
38
- # The `template_extension` is the extension Mustache uses when looking
39
- # for template files. By default it is "mustache"
40
- #
41
- # * template_file
42
- #
43
- # You can tell Mustache exactly which template to us with this
44
- # setting. It can be a relative or absolute path.
45
- #
46
- # * template
47
- #
48
- # Sometimes you want Mustache to render a string, not a file. In those
49
- # cases you may set the `template` setting. For example:
50
- #
51
- # >> Mustache.render("Hello {{planet}}", :planet => "World!")
52
- # => "Hello World!"
53
- #
54
- # The `template` setting is also available on instances.
55
- #
56
- # view = Mustache.new
57
- # view.template = "Hi, {{person}}!"
58
- # view[:person] = 'Mom'
59
- # view.render # => Hi, mom!
60
- #
61
- # * view_namespace
62
- #
63
- # To make life easy on those developing Mustache plugins for web frameworks or
64
- # other libraries, Mustache will attempt to load view classes (i.e. Mustache
65
- # subclasses) using the `view_class` class method. The `view_namespace` tells
66
- # Mustache under which constant view classes live. By default it is `Object`.
67
- #
68
- # * view_path
69
- #
70
- # Similar to `template_path`, the `view_path` option tells Mustache where to look
71
- # for files containing view classes when using the `view_class` method.
72
- #
4
+
73
5
  module MustacheRender
74
6
  class Mustache
75
-
76
- #
77
- # Public API
78
- #
79
-
80
- # Instantiates an instance of this class and calls `render` with
81
- # the passed args.
82
- #
83
- # Returns a rendered String version of a template
84
7
  def self.render(*args)
85
8
  new.render(*args)
86
9
  end
87
10
 
88
- class << self
89
- alias_method :to_html, :render
90
- alias_method :to_text, :render
11
+ attr_reader :media ## 模板的媒介
12
+
13
+ def config
14
+ ::MustacheRender.config
91
15
  end
92
16
 
93
- # Parses our fancy pants template file and returns normal file with
94
- # all special {{tags}} and {{#sections}}replaced{{/sections}}.
95
- #
96
- # data - A String template or a Hash context. If a Hash is given,
97
- # we'll try to figure out the template from the class.
98
- # ctx - A Hash context if `data` is a String template.
99
- #
100
- # Examples
101
- #
102
- # @view.render("Hi {{thing}}!", :thing => :world)
103
- #
104
- # View.template = "Hi {{thing}}!"
105
- # @view = View.new
106
- # @view.render(:thing => :world)
107
- #
108
- # Returns a rendered String version of a template
109
- def render(data = template, ctx = {})
110
- if data.is_a? Hash
111
- ctx = data
112
- tpl = templateify(template)
113
- elsif data.is_a? Symbol
114
- self.template_name = data
115
- tpl = templateify(template)
116
- else
117
- tpl = templateify(data)
118
- end
17
+ def media
18
+ @media ||= config.default_render_media
19
+ end
119
20
 
21
+ def render(data = template, ctx = {})
22
+ tpl = templateify(data)
23
+
120
24
  return tpl.render(context) if ctx == {}
121
25
 
122
26
  begin
@@ -127,9 +31,6 @@ module MustacheRender
127
31
  end
128
32
  end
129
33
 
130
- alias_method :to_html, :render
131
- alias_method :to_text, :render
132
-
133
34
  # Context accessors.
134
35
  #
135
36
  # view = Mustache.new
@@ -153,37 +54,41 @@ module MustacheRender
153
54
 
154
55
  # Given a file name and an optional context, attempts to load and
155
56
  # render the file as a template.
156
- def self.render_file(name, context = {})
157
- render(partial(name), context)
57
+ def self.file_render(name, context = {})
58
+ self.new.file_render name, context
158
59
  end
159
60
 
160
61
  # Given a file name and an optional context, attempts to load and
161
62
  # render the file as a template.
162
- def render_file(name, context = {})
163
- self.class.render_file(name, context)
63
+ def file_render(name, context = {})
64
+ @media = :file
65
+ render(partial(name), context)
164
66
  end
165
67
 
166
- def self.read_template(name)
167
- db_template = ::MustacheRenderTemplate.find_with_full_path(name)
168
- db_template.try :content
68
+ def self.db_render(full_path, context={})
69
+ self.new.db_render full_path, context
169
70
  end
170
71
 
171
- # Given a name, attempts to read a file and return the contents as a
172
- # string. The file is not rendered, so it might contain
173
- # {{mustaches}}.
174
- #
175
- # Call `render` if you need to process it.
176
- def self.partial(name)
177
- self.read_template(name)
72
+ def db_render(full_path, context={})
73
+ @media = :db
74
+ render(partial(full_path), context)
75
+ end
178
76
 
179
- # File.read("#{template_path}/#{name}.#{template_extension}")
77
+ def read_template_from_meida name
78
+ case media
79
+ when :db
80
+ db_template = ::MustacheRenderTemplate.find_with_full_path(name)
81
+ db_template.try :content
82
+ when :file
83
+ File.read "#{config.file_template_root_path}/#{name}.#{config.file_template_extension}"
84
+ end
180
85
  end
181
86
 
182
87
  # Override this in your subclass if you want to do fun things like
183
88
  # reading templates from a database. It will be rendered by the
184
89
  # context, so all you need to do is return a string.
185
90
  def partial(name)
186
- self.class.partial(name)
91
+ self.read_template_from_meida name
187
92
  end
188
93
 
189
94
  # Override this to provide custom escaping.
@@ -199,7 +104,6 @@ module MustacheRender
199
104
  CGI.escapeHTML(str)
200
105
  end
201
106
 
202
-
203
107
  #
204
108
  # Private API
205
109
  #
@@ -3,7 +3,7 @@ module MustacheRender
3
3
  VERSION_NUMBERS = [
4
4
  VERSION_MAJOR = 0,
5
5
  VERSION_MINOR = 0,
6
- VERSION_BUILD = 3,
6
+ VERSION_BUILD = 4,
7
7
  ]
8
8
 
9
9
  VERSION = VERSION_NUMBERS.join(".")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache_render
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - happy
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-11 00:00:00 +08:00
18
+ date: 2013-01-12 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -82,7 +82,6 @@ files:
82
82
  - lib/mustache_render/mustache/context.rb
83
83
  - lib/mustache_render/mustache/template.rb
84
84
  - lib/mustache_render/mustache/parser.rb
85
- - lib/mustache_render/mustache/settings.rb
86
85
  - lib/mustache_render/controllers/mustache_render/manager/base_controller.rb
87
86
  - lib/mustache_render/controllers/mustache_render/manager/templates_controller.rb
88
87
  - lib/mustache_render/controllers/mustache_render/manager/folders_controller.rb
@@ -1,234 +0,0 @@
1
- # Settings which can be configured for all view classes, a single
2
- # view class, or a single Mustache instance.
3
- module MustacheRender
4
- class Mustache
5
-
6
- #
7
- # Template Path
8
- #
9
-
10
- # The template path informs your Mustache view where to look for its
11
- # corresponding template. By default it's the current directory (".")
12
- #
13
- # A class named Stat with a template_path of "app/templates" will look
14
- # for "app/templates/stat.mustache"
15
-
16
- def self.template_path
17
- @template_path ||= inheritable_config_for :template_path, '.'
18
- end
19
-
20
- def self.template_path=(path)
21
- @template_path = File.expand_path(path)
22
- @template = nil
23
- end
24
-
25
- def template_path
26
- @template_path ||= self.class.template_path
27
- end
28
-
29
- def template_path=(path)
30
- @template_path = File.expand_path(path)
31
- @template = nil
32
- end
33
-
34
- # Alias for `template_path`
35
- def self.path
36
- template_path
37
- end
38
- alias_method :path, :template_path
39
-
40
- # Alias for `template_path`
41
- def self.path=(path)
42
- self.template_path = path
43
- end
44
- alias_method :path=, :template_path=
45
-
46
-
47
- #
48
- # Template Extension
49
- #
50
-
51
- # A Mustache template's default extension is 'mustache', but this can be changed.
52
-
53
- def self.template_extension
54
- @template_extension ||= inheritable_config_for :template_extension, 'mustache'
55
- end
56
-
57
- def self.template_extension=(template_extension)
58
- @template_extension = template_extension
59
- @template = nil
60
- end
61
-
62
- def template_extension
63
- @template_extension ||= self.class.template_extension
64
- end
65
-
66
- def template_extension=(template_extension)
67
- @template_extension = template_extension
68
- @template = nil
69
- end
70
-
71
-
72
- #
73
- # Template Name
74
- #
75
-
76
- # The template name is the Mustache template file without any
77
- # extension or other information. Defaults to `class_name`.
78
- #
79
- # You may want to change this if your class is named Stat but you want
80
- # to re-use another template.
81
- #
82
- # class Stat
83
- # self.template_name = "graphs" # use graphs.mustache
84
- # end
85
-
86
- def self.template_name
87
- @template_name || underscore
88
- end
89
-
90
- def self.template_name=(template_name)
91
- @template_name = template_name
92
- @template = nil
93
- end
94
-
95
- def template_name
96
- @template_name ||= self.class.template_name
97
- end
98
-
99
- def template_name=(template_name)
100
- @template_name = template_name
101
- @template = nil
102
- end
103
-
104
-
105
- #
106
- # Template File
107
- #
108
-
109
- # The template file is the absolute path of the file Mustache will
110
- # use as its template. By default it's ./class_name.mustache
111
-
112
- # FIXME: remove here! for db store ############# happy ###
113
- # def self.template_file
114
- # @template_file || "#{path}/#{template_name}.#{template_extension}"
115
- # end
116
-
117
- # def self.template_file=(template_file)
118
- # @template_file = template_file
119
- # @template = nil
120
- # end
121
-
122
- # # The template file is the absolute path of the file Mustache will
123
- # # use as its template. By default it's ./class_name.mustache
124
- # def template_file
125
- # @template_file || "#{path}/#{template_name}.#{template_extension}"
126
- # end
127
-
128
- # def template_file=(template_file)
129
- # @template_file = template_file
130
- # @template = nil
131
- # end
132
-
133
-
134
- #
135
- # Template
136
- #
137
-
138
- # The template is the actual string Mustache uses as its template.
139
- # There is a bit of magic here: what we get back is actually a
140
- # Mustache::Template object, but you can still safely use `template=`
141
- # with a string.
142
-
143
- def self.template
144
- @template ||= templateify(self.read_template(self.template_name))
145
- # @template ||= templateify(File.read(template_file))
146
- end
147
-
148
- def self.template=(template)
149
- @template = templateify(template)
150
- end
151
-
152
- # The template can be set at the instance level.
153
- def template
154
- return @template if @template
155
-
156
- ## FIXME: add here
157
- @template = self.class.template
158
-
159
- # FIXME: remove here for db:store
160
- # # If they sent any instance-level options use that instead of the class's.
161
- # if @template_path || @template_extension || @template_name || @template_file
162
- # @template = templateify(File.read(template_file))
163
- # else
164
- # @template = self.class.template
165
- # end
166
- end
167
-
168
- def template=(template)
169
- @template = templateify(template)
170
- end
171
-
172
-
173
- #
174
- # Raise on context miss
175
- #
176
-
177
- # Should an exception be raised when we cannot find a corresponding method
178
- # or key in the current context? By default this is false to emulate ctemplate's
179
- # behavior, but it may be useful to enable when debugging or developing.
180
- #
181
- # If set to true and there is a context miss, `Mustache::ContextMiss` will
182
- # be raised.
183
-
184
- def self.raise_on_context_miss?
185
- @raise_on_context_miss
186
- end
187
-
188
- def self.raise_on_context_miss=(boolean)
189
- @raise_on_context_miss = boolean
190
- end
191
-
192
- # Instance level version of `Mustache.raise_on_context_miss?`
193
- def raise_on_context_miss?
194
- self.class.raise_on_context_miss? || @raise_on_context_miss
195
- end
196
-
197
- def raise_on_context_miss=(boolean)
198
- @raise_on_context_miss = boolean
199
- end
200
-
201
-
202
- #
203
- # View Namespace
204
- #
205
-
206
- # The constant under which Mustache will look for views when autoloading.
207
- # By default the view namespace is `Object`, but it might be nice to set
208
- # it to something like `Hurl::Views` if your app's main namespace is `Hurl`.
209
-
210
- def self.view_namespace
211
- @view_namespace ||= inheritable_config_for(:view_namespace, Object)
212
- end
213
-
214
- def self.view_namespace=(namespace)
215
- @view_namespace = namespace
216
- end
217
-
218
-
219
- #
220
- # View Path
221
- #
222
-
223
- # Mustache searches the view path for .rb files to require when asked to find a
224
- # view class. Defaults to "."
225
-
226
- def self.view_path
227
- @view_path ||= inheritable_config_for(:view_path, '.')
228
- end
229
-
230
- def self.view_path=(path)
231
- @view_path = path
232
- end
233
- end
234
- end