Method: OpenC3::TargetModel#render

Defined in:
lib/openc3/models/target_model.rb

#render(template_name, options = {}) ⇒ Object

Called by the ERB template to render a partial



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/openc3/models/target_model.rb', line 713

def render(template_name, options = {})
  raise "Partial name '#{template_name}' must begin with an underscore." if File.basename(template_name)[0] != '_'

  b = binding
  b.local_variable_set(:target_name, @name)
  if options[:locals]
    options[:locals].each { |key, value| b.local_variable_set(key, value) }
  end

  # Assume the file is there. If not we raise a pretty obvious error
  if File.expand_path(template_name) == template_name # absolute path
    path = template_name
  else # relative to the current @filename
    path = File.join(File.dirname(@filename), template_name)
  end

  data = File.read(path, mode: "rb")
  erb_disabled = check_disable_erb(path)
  begin
    if erb_disabled
      return data
    else
      OpenC3.set_working_dir(File.dirname(path)) do
        return ERB.new(data.force_encoding("UTF-8").comment_erb(), trim_mode: "-").result(b)
      end
    end
  rescue => e
    raise "ERB error parsing: #{path}: #{e.formatted}"
  end
end