Module: Inline::Erb

Defined in:
lib/inline/erb.rb,
lib/inline/erb/version.rb

Defined Under Namespace

Classes: TemplateRenderer

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.render(name, **context) ⇒ Object

Reads from a embedded template

Examples:

inline_template('vhost.conf',
                server_name: "example.com")

Parameters:

  • src

    String - The data found after __END__

  • context

    Hash - variables to pass into template

Returns:

  • Rendered Template



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inline/erb.rb', line 31

def render(name, **context)
  templates = {}
  begin
    app, data = File.read(caller.first.split(":").first).split("__END__", 2)
  rescue Errno::ENOENT
    app, data = nil
  end

  data.strip!
  if data
    template = nil
    data.each_line do |line|
      if line =~ /^@@\s*(.*\S)\s*$/
        template = String.new
        templates[$1.to_s] = template
      elsif
        template << line
      end
    end

    TemplateRenderer.render(templates[name], context)
  end
end