42 lines
943 B
Ruby
42 lines
943 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'erb'
|
|
|
|
require_relative 'cache'
|
|
|
|
module FO
|
|
class Renderer
|
|
def initialize(layout, template, locals)
|
|
@layout = layout
|
|
@template = template
|
|
@locals = locals
|
|
@globals = {title: 'fullodds.club'}
|
|
@cache = FO::Cache.new
|
|
end
|
|
|
|
def render
|
|
layout_content = @cache.file!(@layout)
|
|
template_content = @cache.file!(@template)
|
|
@locals.merge(@globals).each do |k,v|
|
|
self.instance_variable_set("@#{k.to_s}".to_sym, v)
|
|
end
|
|
b = binding
|
|
b.local_variable_set(:content, ERB.new(template_content).result(b))
|
|
ERB.new(layout_content).result(b)
|
|
end
|
|
|
|
private
|
|
|
|
def verify_templates!
|
|
unless File.exist?(@layout)
|
|
raise ArgumentError, "Layout #{@layout} does not exist!"
|
|
end
|
|
|
|
unless File.exist?(@template)
|
|
raise ArgumentError, "Template #{@template} does not exist!"
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|