Sha256: 779da3d1b54ef18ee5894956b02bff0a88c8bb6c1bd027a3de9446b65b03fc00

Contents?: true

Size: 1.38 KB

Versions: 31

Compression:

Stored size: 1.38 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(@blueprint, @options)
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
lono-6.1.11 lib/lono/template/template.rb
lono-6.1.10 lib/lono/template/template.rb
lono-6.1.9 lib/lono/template/template.rb
lono-6.1.8 lib/lono/template/template.rb
lono-6.1.7 lib/lono/template/template.rb
lono-6.1.6 lib/lono/template/template.rb
lono-6.1.5 lib/lono/template/template.rb
lono-6.1.4 lib/lono/template/template.rb
lono-6.1.3 lib/lono/template/template.rb
lono-6.1.2 lib/lono/template/template.rb
lono-6.1.1 lib/lono/template/template.rb
lono-6.1.0 lib/lono/template/template.rb
lono-6.0.1 lib/lono/template/template.rb
lono-6.0.0 lib/lono/template/template.rb
lono-5.3.4 lib/lono/template/template.rb
lono-5.3.3 lib/lono/template/template.rb
lono-5.3.2 lib/lono/template/template.rb
lono-5.3.1 lib/lono/template/template.rb
lono-5.3.0 lib/lono/template/template.rb
lono-5.2.8 lib/lono/template/template.rb