Class: Govspeak::TemplateRenderer
- Inherits:
-
Object
- Object
- Govspeak::TemplateRenderer
- Defined in:
- lib/govspeak/template_renderer.rb
Instance Attribute Summary collapse
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #format_with_html_line_breaks(string) ⇒ Object
-
#initialize(template, locale) ⇒ TemplateRenderer
constructor
A new instance of TemplateRenderer.
- #render(locals) ⇒ Object
- #t(*args) ⇒ Object
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
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
3 4 5 |
# File 'lib/govspeak/template_renderer.rb', line 3 def locale @locale end |
#template ⇒ Object (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) = args.last.is_a?(Hash) ? args.last.dup : {} key = args.shift I18n.t!(key, **.merge(locale:)) end |