Class: Govspeak::TemplateRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/govspeak/template_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, locale) ⇒ TemplateRenderer

Returns a new instance of TemplateRenderer.



5
6
7
8
# File 'lib/govspeak/template_renderer.rb', line 5

def initialize(template, locale)
  @template = template
  @locale = locale
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



3
4
5
# File 'lib/govspeak/template_renderer.rb', line 3

def locale
  @locale
end

#templateObject (readonly)

Returns the value of attribute template.



3
4
5
# File 'lib/govspeak/template_renderer.rb', line 3

def template
  @template
end

Instance Method Details

#format_with_html_line_breaks(string) ⇒ Object



23
24
25
# File 'lib/govspeak/template_renderer.rb', line 23

def format_with_html_line_breaks(string)
  ERB::Util.html_escape(string || "").strip.gsub(/(?:\r?\n)/, "<br/>").html_safe
end

#render(locals) ⇒ Object



10
11
12
13
14
15
# File 'lib/govspeak/template_renderer.rb', line 10

def render(locals)
  template_binding = binding
  locals.each { |k, v| template_binding.local_variable_set(k, v) }
  erb = ERB.new(File.read(__dir__ + "/../templates/#{template}"))
  erb.result(template_binding)
end

#t(*args) ⇒ Object



17
18
19
20
21
# File 'lib/govspeak/template_renderer.rb', line 17

def t(*args)
  options = args.last.is_a?(Hash) ? args.last.dup : {}
  key = args.shift
  I18n.t!(key, **options.merge(locale:))
end