Module: Lydown::Templates

Defined in:
lib/lydown/templates.rb

Constant Summary collapse

TEMPLATES_DIR =
File.join(File.dirname(__FILE__), 'templates')
@@templates =
{}

Class Method Summary collapse

Class Method Details

.load_template(name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lydown/templates.rb', line 20

def self.load_template(name)
  fn = name.is_a?(Symbol) ?
    File.join(TEMPLATES_DIR, "#{name}.erb") :
    name

  ERB.new IO.read(fn), 0, '<>'
end

.render(name, ctx, locals = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/lydown/templates.rb', line 9

def self.render(name, ctx, locals = {})
  _binding = ctx.respond_to?(:template_binding) ? ctx.template_binding(locals) : binding

  # remove trailing white space and superfluous new lines
  template(name).result(_binding).gsub(/^\s+$/, '').gsub(/\n+/m, "\n")
end

.template(name) ⇒ Object



16
17
18
# File 'lib/lydown/templates.rb', line 16

def self.template(name)
  @@templates[name] ||= load_template(name)
end