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.7 lib/ree/templates/template_renderer.rb
ree-1.0.6 lib/ree/templates/template_renderer.rb
ree-1.0.5 lib/ree/templates/template_renderer.rb
ree-1.0.4 lib/ree/templates/template_renderer.rb
ree-1.0.3 lib/ree/templates/template_renderer.rb
ree-1.0.2 lib/ree/templates/template_renderer.rb
ree-1.0.1 lib/ree/templates/template_renderer.rb
ree-1.0.0 lib/ree/templates/template_renderer.rb