class Yokunai::Template
Renders ERB templates.
Constants
- FALLBACK_EMPTY_LAYOUT
Public Class Methods
new(template_path: nil)
click to toggle source
# File lib/yokunai/template.rb, line 7 def initialize(template_path: nil) template_dir = File.join(Yokunai::Config.base_dir, Yokunai::Config.get("template_dir")) @template_path = template_path || template_dir @raw_layout = get_layout end
Public Instance Methods
exist?(template)
click to toggle source
Checks if a template exists. Useful for gating before rendering.
@param template [String] the template name @return [Boolean]
# File lib/yokunai/template.rb, line 32 def exist?(template) File.exist?(File.join(@template_path, template + ".erb")) end
render(template, context = {})
click to toggle source
Render an ERB template with the given name, and cache the result for subsequent calls.
@param template [String] the name of a template @param context [Hash] key/value pairs of variables to bind the template @return [String] the ERB render result
# File lib/yokunai/template.rb, line 19 def render(template, context = {}) return nil unless exist?(template) path = File.join(@template_path, template + ".erb") layout_context = context.merge(partial: ERB.new(File.read(path)).result(Yokunai::RenderContext.new(context).get_binding)) ERB.new(@raw_layout).result(Yokunai::RenderContext.new(layout_context).get_binding) end
Private Instance Methods
get_layout()
click to toggle source
# File lib/yokunai/template.rb, line 38 def get_layout layout_path = File.join(@template_path, "layout.erb") if File.exist?(layout_path) File.read(layout_path) else FALLBACK_EMPTY_LAYOUT end end