Sha256: c2ca4a2124ee51ca9448976f96489f2ba52987cecfeb2655cef1d9e58f546aed

Contents?: true

Size: 654 Bytes

Versions: 4

Compression:

Stored size: 654 Bytes

Contents

class Template
  @_cache = {}
  def self.[](name)
    @_cache[name]
  end

  def self.[]=(name, instance)
    @_cache[name] = instance
  end

  def self.paths
    @_cache.keys
  end

  attr_reader :body

  def initialize(name, &body)
    @name, @body = name, body
    Template[name] = self
  end

  def inspect
    "#<Template: '#@name'>"
  end

  def render(ctx = self)
    ctx.instance_exec(OutputBuffer.new, &@body)
  end

  class OutputBuffer
    def initialize
      @buffer = []
    end

    def append(str)
      @buffer << str
    end

    def append=(content)
      @buffer << content
    end

    def join
      @buffer.join
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opal-0.5.5 stdlib/template.rb
opal-0.5.4 stdlib/template.rb
opal-0.5.2 stdlib/template.rb
opal-0.5.0 stdlib/template.rb