Class: Ditz::ErbHtml
Overview
pass through any variables needed for template generation, and add a bunch of HTML formatting utility methods.
Instance Method Summary
collapse
Constructor Details
#initialize(template_dir, template_name, links, mapping = {}) ⇒ ErbHtml
Returns a new instance of ErbHtml.
8
9
10
11
12
13
14
15
16
|
# File 'lib/html.rb', line 8
def initialize template_dir, template_name, links, mapping={}
@template_name = template_name
@template_dir = template_dir
@links = links
@mapping = mapping
@@erbs ||= {}
@@erbs[template_name] ||= ERB.new(IO.readlines(File.join(template_dir, "#{template_name}.rhtml")).join)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *a) ⇒ Object
32
33
34
|
# File 'lib/html.rb', line 32
def method_missing meth, *a
@mapping.member?(meth) ? @mapping[meth] : super
end
|
Instance Method Details
18
|
# File 'lib/html.rb', line 18
def h o; o.to_s.gsub("&", "&").gsub("<", "<").gsub(">", ">") end
|
#link_to(o, name) ⇒ Object
21
22
23
24
25
26
|
# File 'lib/html.rb', line 21
def link_to o, name
dest = @links[o]
dest = o if dest.nil? && o.is_a?(String)
raise ArgumentError, "no link for #{o.inspect}" unless dest
"<a href=\"#{dest}\">#{name}</a>"
end
|
#obscured_email(e) ⇒ Object
20
|
# File 'lib/html.rb', line 20
def obscured_email e; h e.gsub(/@.*?(>|$)/, "@...\\1") end
|
19
|
# File 'lib/html.rb', line 19
def p o; "<p>" + h(o.to_s).gsub("\n\n", "</p><p>") + "</p>" end
|
#render(template_name, morevars = {}) ⇒ Object
28
29
30
|
# File 'lib/html.rb', line 28
def render template_name, morevars={}
ErbHtml.new(@template_dir, template_name, @links, @mapping.merge(morevars)).to_s
end
|
36
37
38
|
# File 'lib/html.rb', line 36
def to_s
@@erbs[@template_name].result binding
end
|