Sha256: 2ad1bdce426de33f855ee17f47ca49032c612151d3d6a859c6be1f6d5c3139d9

Contents?: true

Size: 1.32 KB

Versions: 48

Compression:

Stored size: 1.32 KB

Contents

class Ree::TemplateRenderer
  TEMPLATE_VARIABLE = /<%=\s*([\w\d-]+)\s*%>/i

  class << self
    def handle(template, locals = {})
      Ree::TemplateRenderer.new(template, locals).handle
    end

    def get_undefined_variables(template, locals = {})
      Ree::TemplateRenderer.new(template, locals).get_undefined_variables
    end
  end

  def initialize(template, locals)
    @template = template
    @locals   = locals
    @undefined_variables = []
  end

  def handle
    raise Ree::Error.new("Template variable not defined: #{get_undefined_variables.map(&:to_s).join(';')}") if get_undefined_variables.any?

    Ree::RenderUtils.render(@template, @locals)
  end

  def get_undefined_variables
    Ree::RenderUtils.render(@template, @locals)

    @undefined_variables
  rescue NameError => e
    raise NoMethodError, "Undefined method for template. Please, add :#{e.name} method to .ree/helpers/render_helper.rb file!" if @undefined_variables.include?(e.name)

    @locals[e.name] = e.name.to_s
    @undefined_variables.push(e.name)

    retry
  end

  private

  def get_template_variables
    Ree::RenderUtils.render(@template, {})
  rescue NameError => e
    @locals

    get_template_variables(@template, fake_locals, undefined_variables)
  end

  def get_passed_variables
    @locals
      .keys
      .uniq
      .map(&:intern)
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
ree-1.0.47 lib/ree/templates/template_renderer.rb
ree-1.0.46 lib/ree/templates/template_renderer.rb
ree-1.0.45 lib/ree/templates/template_renderer.rb
ree-1.0.44 lib/ree/templates/template_renderer.rb
ree-1.0.43 lib/ree/templates/template_renderer.rb
ree-1.0.42 lib/ree/templates/template_renderer.rb
ree-1.0.41 lib/ree/templates/template_renderer.rb
ree-1.0.40 lib/ree/templates/template_renderer.rb
ree-1.0.39 lib/ree/templates/template_renderer.rb
ree-1.0.38 lib/ree/templates/template_renderer.rb
ree-1.0.37 lib/ree/templates/template_renderer.rb
ree-1.0.36 lib/ree/templates/template_renderer.rb
ree-1.0.35 lib/ree/templates/template_renderer.rb
ree-1.0.34 lib/ree/templates/template_renderer.rb
ree-1.0.33 lib/ree/templates/template_renderer.rb
ree-1.0.32 lib/ree/templates/template_renderer.rb
ree-1.0.31 lib/ree/templates/template_renderer.rb
ree-1.0.30 lib/ree/templates/template_renderer.rb
ree-1.0.29 lib/ree/templates/template_renderer.rb
ree-1.0.28 lib/ree/templates/template_renderer.rb