Sha256: f0425aae6b98da4120e7aedb3bd44441c05e31544cabe87084cbf47fef1cf353

Contents?: true

Size: 1.36 KB

Versions: 29

Compression:

Stored size: 1.36 KB

Contents

require 'erb'
require 'json'
require 'base64'

class Lono::Template::Template
  include ERB::Util

  # Main template Erb methods are: source and variables
  #
  #   template "example-2" do
  #     source "example"
  #     variables(test: 1)
  #   end
  #
  attr_reader :name
  def initialize(blueprint, name, block=nil, options={})
    @blueprint, @name, @block, @options = blueprint, name, block, options
    @source_path = default_source_path(name)
  end

  # Returns path, example: ./app/templates/example.yml
  def source(path)
    @source_path = path[0..0] == '/' ? path : "#{Lono.config.templates_path}/#{path}"
    @source_path += ".yml"
  end

  def variables(vars={})
    vars.each do |var,value|
      context.instance_variable_set("@#{var}", value)
    end
  end

  # internal methods
  def default_source_path(name)
    "#{Lono.config.templates_path}/#{name}.yml" # defaults to name, source method overrides
  end

  def build
    instance_eval(&@block) if @block

    if File.exist?(@source_path)
      RenderMePretty.result(@source_path, context: context)
    else
      puts "ERROR: #{@source_path} does not exist, but it was used as a template source.".color(:red)
      exit 1
    end
  end

  # Context for ERB rendering.
  # This is where we control what references get passed to the ERB rendering.
  def context
    @context ||= Lono::Template::Context.new(@options)
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
lono-7.5.2 lib/lono/template/template.rb
lono-7.5.1 lib/lono/template/template.rb
lono-7.5.0 lib/lono/template/template.rb
lono-7.4.11 lib/lono/template/template.rb
lono-7.4.10 lib/lono/template/template.rb
lono-7.4.9 lib/lono/template/template.rb
lono-7.4.8 lib/lono/template/template.rb
lono-7.4.7 lib/lono/template/template.rb
lono-7.4.6 lib/lono/template/template.rb
lono-7.4.5 lib/lono/template/template.rb
lono-7.4.4 lib/lono/template/template.rb
lono-7.4.3 lib/lono/template/template.rb
lono-7.4.2 lib/lono/template/template.rb
lono-7.4.1 lib/lono/template/template.rb
lono-7.4.0 lib/lono/template/template.rb
lono-7.3.2 lib/lono/template/template.rb
lono-7.3.1 lib/lono/template/template.rb
lono-7.3.0 lib/lono/template/template.rb
lono-7.2.3 lib/lono/template/template.rb
lono-7.2.2 lib/lono/template/template.rb