Sha256: 82321d8d6eb301386ae70f851083307609df097ea38a7d989b422fc8cb6589e5

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "okay/version"
require "okay/warning_helpers"
require "pathname"

module Okay
  ##
  # An extremely simple templating engine.
  #
  # General usage:
  #
  #     template = Okay::Template.new("./templates")
  #     puts template.apply("some_template.html", {"some_key": "some value"})
  #     template.directory #=> "./templates"
  class Template
    include WarningHelpers

    attr_reader :directory

    ##
    # Create an Okay::Templates object.
    #
    # @param directory [String] Path of the directory containing the templates.
    def initialize(directory)
      @directory = directory
    end

    ##
    # Apply the template referenced by +template_name+ to +data+.
    #
    # @param template_name [String] Name of the template to use,
    #   relative to +@directory+ (as passed to +#initialize+).
    # @param data [Hash] Data to apply the template to.
    #
    # @return [String] Result of applying the template to +data+.
    def apply(template_name, data)
      template_file = Pathname.new(@directory).join(template_name)
      template = template_file.read

      # Silence warnings while applying the template, since we don't
      # generally care about unused keys.
      silence_warnings { Kernel.format(template, data) }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
okay-12.0.4 lib/okay/template.rb
okay-12.0.3 lib/okay/template.rb
okay-12.0.2 lib/okay/template.rb
okay-12.0.1 lib/okay/template.rb
okay-12.0.0 lib/okay/template.rb
okay-11.0.0 lib/okay/template.rb
okay-10.0.0 lib/okay/template.rb
okay-9.0.0 lib/okay/template.rb