Sha256: 5f7aa373071acb5d825af915af91efe3a2e734e3542ff6457f6046bbc7fbb49b

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require_relative "application/helper"

module SuperSettings
  # Simple class for rendering ERB templates for the HTML application.
  class Application
    include Helper

    # @param layout [String, Symbol] path to an ERB template to use as the layout around the application UI. You can
    #                                pass the symbol +:default+ to use the default layout that ships with the gem.
    # @param add_to_head [String] HTML code to add to the <head> element on the page.
    def initialize(layout = nil, add_to_head = nil)
      if layout
        layout = File.expand_path(File.join("application", "layout.html.erb"), __dir__) if layout == :default
        @layout = ERB.new(File.read(layout))
        @add_to_head = add_to_head
      end
    end

    # Render the specified ERB file in the lib/application directory distributed with the gem.
    #
    # @return [void]
    def render(erb_file)
      template = ERB.new(File.read(File.expand_path(File.join("application", erb_file), __dir__)))
      html = template.result(binding)
      if @layout
        render_layout { html }
      else
        html
      end
    end

    private

    def render_layout
      @layout.result(binding)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
super_settings-1.0.2 lib/super_settings/application.rb
super_settings-1.0.1 lib/super_settings/application.rb
super_settings-1.0.0 lib/super_settings/application.rb
super_settings-0.0.1.rc3 lib/super_settings/application.rb
super_settings-0.0.1.rc2 lib/super_settings/application.rb
super_settings-0.0.1.rc1 lib/super_settings/application.rb