Class: Bloggit::Template
- Inherits:
-
Object
show all
- Extended by:
- Test::Unit::Assertions
- Includes:
- Test::Unit::Assertions
- Defined in:
- lib/bloggit/template.rb
Overview
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](key) ⇒ Object
-
#capture(*args, &block) ⇒ Object
-
#content_for(name, content = nil, &block) ⇒ Object
-
#initialize(file_path, content = nil) ⇒ Template
constructor
A new instance of Template.
-
#link_to(title, params = {}) ⇒ Object
-
#link_to_blog(title, params = {}) ⇒ Object
-
#link_to_feed(title, params = {}) ⇒ Object
-
#link_to_home(title, params = {}) ⇒ Object
-
#media_link_to(media_path, params = {}) ⇒ Object
-
#method_missing(name, *args) ⇒ Object
-
#path_to_root(absolute = false) ⇒ Object
-
#render(params = {}) ⇒ Object
-
#render_binding ⇒ Object
-
#site ⇒ Object
def link_to_archive(title, params={}) atts = “” params.each {|k,v| atts << %Q|#{k}=“#vv.to_s”|} %Q|<a #atts href=“#Template.path_to_rootindex.html”>#title</a>| end.
-
#tag_links(obj, absolute = false) ⇒ Object
-
#url_for(params = {}) ⇒ Object
-
#url_for_blog(params = {}) ⇒ Object
-
#url_for_feed(params = {}) ⇒ Object
-
#url_for_home(params = {}) ⇒ Object
Constructor Details
#initialize(file_path, content = nil) ⇒ Template
Returns a new instance of Template.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/bloggit/template.rb', line 12
def initialize(file_path, content=nil)
@file_path = file_path
@settings = {}
@template_src = ""
@content_parts = {}
unless file_path.nil?
@yaml_parts = YAML::load_stream( File.open(file_path, 'r') )
@yaml_parts.documents.each do |yml|
if yml.is_a? Hash
@settings.merge!(yml)
elsif yml.is_a? String
@template_src << yml.to_s
else
puts "TEMPLATE: Uh, not sure what to do with this part"
end
end
else
@template_src = content
end
@template = ERB.new(@template_src, 0, "%<>")
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/bloggit/template.rb', line 195
def method_missing(name, *args)
if @params.has_key? name.to_s
@params[name.to_s]
elsif @params.has_key? name.to_sym
@params[name.to_sym]
elsif Template.tag_registered?(name)
Template.execute_tag(name, self, *args)
else
raise "Template Error: No method/data found for '#{name}' in template #{file_path}"
end
end
|
Class Attribute Details
.content_parts ⇒ Object
Returns the value of attribute content_parts.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def content_parts
@content_parts
end
|
.current_template ⇒ Object
Returns the value of attribute current_template.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def current_template
@current_template
end
|
.folder_depth ⇒ Object
Returns the value of attribute folder_depth.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def folder_depth
@folder_depth
end
|
.force_absolute_path ⇒ Object
Returns the value of attribute force_absolute_path.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def force_absolute_path
@force_absolute_path
end
|
.site ⇒ Object
Returns the value of attribute site.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def site
@site
end
|
.src_path ⇒ Object
Returns the value of attribute src_path.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def src_path
@src_path
end
|
.template_dir ⇒ Object
Returns the value of attribute template_dir.
263
264
265
|
# File 'lib/bloggit/template.rb', line 263
def template_dir
@template_dir
end
|
Instance Attribute Details
#content_parts ⇒ Object
Returns the value of attribute content_parts.
10
11
12
|
# File 'lib/bloggit/template.rb', line 10
def content_parts
@content_parts
end
|
#file_path ⇒ Object
Returns the value of attribute file_path.
10
11
12
|
# File 'lib/bloggit/template.rb', line 10
def file_path
@file_path
end
|
#settings ⇒ Object
Returns the value of attribute settings.
10
11
12
|
# File 'lib/bloggit/template.rb', line 10
def settings
@settings
end
|
#template ⇒ Object
Returns the value of attribute template.
10
11
12
|
# File 'lib/bloggit/template.rb', line 10
def template
@template
end
|
#template_src ⇒ Object
Returns the value of attribute template_src.
10
11
12
|
# File 'lib/bloggit/template.rb', line 10
def template_src
@template_src
end
|
#yaml_parts ⇒ Object
Returns the value of attribute yaml_parts.
10
11
12
|
# File 'lib/bloggit/template.rb', line 10
def yaml_parts
@yaml_parts
end
|
Class Method Details
.[](key) ⇒ Object
295
296
297
298
299
300
301
|
# File 'lib/bloggit/template.rb', line 295
def [](key)
if @content_parts.has_key?( key )
@content_parts[key].join
else
"<!-- `#{key}` not available -->"
end
end
|
.[]=(key, value) ⇒ Object
303
304
305
|
# File 'lib/bloggit/template.rb', line 303
def []=(key,value)
set_content_for(key, value)
end
|
.config {|_self| ... } ⇒ Object
267
268
269
270
271
272
273
274
275
|
# File 'lib/bloggit/template.rb', line 267
def config
yield self
assert !@site.nil?, 'Site cannot be nil'
@template_dir = File.join(@site.base_path, 'themes', @site.theme, 'templates') if @template_dir.nil?
@is_ready = true
@template_cache = {}
@content_parts = {}
@force_absolute_path = false
end
|
.execute_tag(name, template, *args) ⇒ Object
337
338
339
340
|
# File 'lib/bloggit/template.rb', line 337
def execute_tag(name, template, *args)
@current_template = template
@custom_tags[name.to_sym].call( *args )
end
|
.from_file(path) ⇒ Object
317
318
319
320
321
|
# File 'lib/bloggit/template.rb', line 317
def from_file(path)
path = File.expand_path(path)
raise "Template file must exist" unless File.exists?( path )
new(path)
end
|
.from_text(text) ⇒ Object
323
324
325
|
# File 'lib/bloggit/template.rb', line 323
def from_text(text)
new(nil, text)
end
|
.generate(template, ctx) ⇒ Object
278
279
280
281
282
|
# File 'lib/bloggit/template.rb', line 278
def generate(template, ctx) assert @is_ready, "Template isn't ready"
@template_cache[template] ||= from_file( "#{template_dir}/#{template.to_s}.rhtml" )
@template_cache[template].render(:template=>ctx).to_s
end
|
.path_to_root(absolute_path = false) ⇒ Object
307
308
309
310
311
312
313
314
315
|
# File 'lib/bloggit/template.rb', line 307
def path_to_root(absolute_path = false)
path_to_root = '../' * @folder_depth
if @force_absolute_path || absolute_path
root_url = @site.settings.syndication.fetch('base_url', '')
[root_url, path_to_root].join('/')
else
path_to_root
end
end
|
.register_tag(name, &block) ⇒ Object
328
329
330
331
|
# File 'lib/bloggit/template.rb', line 328
def register_tag(name, &block)
@custom_tags ||= {}
@custom_tags[name.to_sym] = block
end
|
.render(template, src_path, ctx) ⇒ Object
284
285
286
287
288
|
# File 'lib/bloggit/template.rb', line 284
def render(template, src_path, ctx)
@content_parts.clear
@folder_depth = (src_path == '.') ? 0 : src_path.split(File::SEPARATOR).length
generate(template, ctx)
end
|
.set_content_for(key, value) ⇒ Object
290
291
292
293
|
# File 'lib/bloggit/template.rb', line 290
def set_content_for(key, value)
@content_parts[key] = [] unless @content_parts.has_key? key
@content_parts[key] << value
end
|
.tag_registered?(name) ⇒ Boolean
333
334
335
|
# File 'lib/bloggit/template.rb', line 333
def tag_registered?(name)
@custom_tags.has_key? name.to_sym
end
|
Instance Method Details
#[](key) ⇒ Object
185
186
187
188
189
190
191
192
193
|
# File 'lib/bloggit/template.rb', line 185
def [](key)
if @params.has_key? key.to_s
@params[key.to_s]
elsif @params.has_key? key.to_sym
@params[key.to_sym]
else
super(*args)
end
end
|
#capture(*args, &block) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/bloggit/template.rb', line 207
def capture(*args, &block)
begin
buffer = eval('_erbout', block.binding)
rescue
buffer = nil
end
if buffer.nil?
capture_block(*args, &block).to_s
else
capture_erb_with_buffer(buffer, *args, &block).to_s
end
end
|
#content_for(name, content = nil, &block) ⇒ Object
222
223
224
|
# File 'lib/bloggit/template.rb', line 222
def content_for(name, content = nil, &block)
Template[name] = capture(&block)
end
|
#link_to(title, params = {}) ⇒ Object
136
137
138
139
140
141
142
|
# File 'lib/bloggit/template.rb', line 136
def link_to(title, params={})
url = url_for(params)
title ||= url
atts = ""
params.each {|k,v| atts << %Q|#{k}="#{v.to_s}"|}
%Q|<a #{atts} href="#{url}">#{title}</a>|
end
|
#link_to_blog(title, params = {}) ⇒ Object
150
151
152
153
154
|
# File 'lib/bloggit/template.rb', line 150
def link_to_blog(title, params={})
atts = ""
params.each {|k,v| atts << %Q|#{k}="#{v.to_s}"|}
%Q|<a #{atts} href="#{ url_for_blog }">#{title}</a>|
end
|
#link_to_feed(title, params = {}) ⇒ Object
156
157
158
159
160
|
# File 'lib/bloggit/template.rb', line 156
def link_to_feed(title, params={})
atts = ""
params.each {|k,v| atts << %Q|#{k}="#{v.to_s}"|}
%Q|<a #{atts} href="#{ url_for_feed }">#{title}</a>|
end
|
#link_to_home(title, params = {}) ⇒ Object
144
145
146
147
148
|
# File 'lib/bloggit/template.rb', line 144
def link_to_home(title, params={})
atts = ""
params.each {|k,v| atts << %Q|#{k}="#{v.to_s}"|}
%Q|<a #{atts} href="#{ url_for_home }">#{title}</a>|
end
|
162
163
164
165
166
167
168
169
|
# File 'lib/bloggit/template.rb', line 162
def media_link_to(media_path, params={})
media_item_url = url_for(:media=>media_path)
size = params.delete(:size).to_s
atts = %Q|border="0" |
atts << %Q|width="#{size}" | if size.ends_with?('%')
image_tag = %Q|<img src="#{media_item_url}" #{atts} />|
link_to image_tag, params
end
|
#path_to_root(absolute = false) ⇒ Object
171
172
173
|
# File 'lib/bloggit/template.rb', line 171
def path_to_root(absolute=false)
Template.path_to_root(absolute)
end
|
#render(params = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/bloggit/template.rb', line 34
def render(params={})
if name = params.fetch(:snippet, nil)
Template.generate "snippets/#{name}", params
elsif ctx = params.fetch(:template, nil)
@params = ctx
if layout_name = @settings.fetch('layout', nil)
output = @template.result( binding )
ctx['content_for_layout'] = output
Template.generate "layouts/#{layout_name}", ctx
else
@template.result( binding )
end
elsif name = params.fetch(:plugin, nil)
puts "RENDER PLUGIN #{name}"
elsif name = params.fetch(:content_for, nil)
Template[name].to_s
elsif obj = params.fetch(:content, nil)
obj.render_content(binding)
elsif content = params.fetch(:text, nil)
Template.from_text(content).template.result(binding)
else
puts "Cannot find renderer for #{params.to_yaml}"
end
end
|
#render_binding ⇒ Object
226
227
228
|
# File 'lib/bloggit/template.rb', line 226
def render_binding
binding
end
|
#site ⇒ Object
def link_to_archive(title, params={})
atts = ""
params.each {|k,v| atts << %Q|#{k}="#{v.to_s}"|}
%Q|<a #{atts} href="#{Template.path_to_root}index.html">#{title}</a>|
end
181
182
183
|
# File 'lib/bloggit/template.rb', line 181
def site
Template.site
end
|
#tag_links(obj, absolute = false) ⇒ Object
116
117
118
119
120
121
122
|
# File 'lib/bloggit/template.rb', line 116
def tag_links(obj, absolute=false)
links = []
obj.tags.each do |tag|
links << link_to(tag, :tag=>tag, :absolute=>absolute)
end
links
end
|
#url_for(params = {}) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/bloggit/template.rb', line 65
def url_for(params={})
absolute = params.fetch(:absolute, false)
if post = params.delete(:post)
if post.is_a? String
Template.path_to_root(absolute) + site.get_post_by_slug(post).permalink
elsif post.is_a? Post
Template.path_to_root(absolute) + post.permalink
end
elsif page = params.delete(:page)
if page.is_a? String
Template.path_to_root(absolute) + site.get_page_by_slug(page).permalink
elsif page.is_a? Page
Template.path_to_root(absolute) + page.permalink
end
elsif tag = params.delete(:tag)
if tag.is_a? String
Template.path_to_root(absolute) + Tag.tag_list[tag].permalink
elsif tag.is_a? Tag
Template.path_to_root(absolute) + tag.permalink
end
elsif media = params.delete(:media)
if media.is_a? String
Template.path_to_root(absolute) + site.media[media].permalink
elsif media.is_a? Media
Template.path_to_root(absolute) + media.permalink
end
elsif script = params.delete(:script)
script += '.js' unless script.ends_with? '.js'
"#{ Template.path_to_root(absolute) }theme/scripts/#{ script }"
elsif style = params.delete(:style)
style += '.css' unless style.ends_with? '.css'
"#{ Template.path_to_root(absolute) }theme/styles/#{ style }"
elsif image = params.delete(:image)
"#{ Template.path_to_root(absolute) }theme/images/#{ image }"
elsif path = params.delete(:from_root)
Template.path_to_root(absolute) + path
elsif url = params.delete(:url)
url
end
end
|
#url_for_blog(params = {}) ⇒ Object
124
125
126
|
# File 'lib/bloggit/template.rb', line 124
def url_for_blog(params={})
"#{ Template.path_to_root(params.fetch(:absolute, false)) }#{site.build_post_path('index.html')}"
end
|
#url_for_feed(params = {}) ⇒ Object
132
133
134
|
# File 'lib/bloggit/template.rb', line 132
def url_for_feed(params={})
"#{ Template.path_to_root(params.fetch(:absolute, false)) }#{site.settings.syndication.filename}"
end
|
#url_for_home(params = {}) ⇒ Object
128
129
130
|
# File 'lib/bloggit/template.rb', line 128
def url_for_home(params={})
"#{ Template.path_to_root(params.fetch(:absolute, false)) }index.html"
end
|