class Lyp::Template
Constants
- EMIT_DELIMITER
- EMIT_RE
- ESCAPED_QUOTE
- IF_END
- IF_RE
- IF_START
- INTERPOLATION_END
- INTERPOLATION_RE
- INTERPOLATION_START
- QUOTE
Public Class Methods
load_templates(path)
click to toggle source
# File lib/lyp/template.rb, line 64 def self.load_templates(path) Dir["#{path}/*.rb"].each {|fn| set(File.basename(fn), IO.read(fn))} end
new(templ)
click to toggle source
# File lib/lyp/template.rb, line 20 def initialize(templ) templ = templ.gsub(EMIT_RE) {|m| convert_literal($1)} method_str = <<EOF define_method(:render) do |_ = {}, env = {}| __buffer__ = env[:buffer] ||= '' __emit__ = env[:emit] ||= lambda {|s| __buffer__ << s} __render_l__ = env[:render] ||= lambda {|n, o| Template.render(n, o, env)} metaclass.instance_eval "define_method(:__render__) {|n, o| __render_l__[n, o]}" begin #{templ} end __buffer__ end EOF metaclass.instance_eval method_str end
render(name, arg = {}, env = {})
click to toggle source
# File lib/lyp/template.rb, line 72 def self.render(name, arg = {}, env = {}) raise unless @@templates[name.to_sym] @@templates[name.to_sym].render(arg, env) end
set(name, templ)
click to toggle source
# File lib/lyp/template.rb, line 68 def self.set(name, templ) @@templates[name.to_sym] = new(templ) end
Public Instance Methods
convert_interpolation(s)
click to toggle source
# File lib/lyp/template.rb, line 38 def convert_interpolation(s) s.gsub(INTERPOLATION_RE) do code = $1.gsub(ESCAPED_QUOTE, QUOTE) "\#{#{code}}" end end
convert_literal(s)
click to toggle source
# File lib/lyp/template.rb, line 45 def convert_literal(s) # look for interpolated values, wrap them with #{} s = s.inspect.gsub(INTERPOLATION_RE) do code = $1.gsub(ESCAPED_QUOTE, QUOTE) "\#{#{code}}" end s = s.gsub(IF_RE) do test, code = $1, $2 test = test.strip.gsub(ESCAPED_QUOTE, QUOTE) "\#\{if #{test}; \"#{code}\"; end}" end "__emit__[#{s}]" end
metaclass()
click to toggle source
From the metaid gem
# File lib/lyp/template.rb, line 18 def metaclass; class << self; self; end; end