Last Updated: February 25, 2016
·
645
· pniemczyk

Document rendering engine

If you need to render some documents from several files with specific data

this can be useful for you

you can build any type of file html, json, txt ...

gem rendering_engine

you can simply

gem install rendering_engine

How it wokrks?

RenderingEngine::Provider can be create with options:

  • base_path base path(required)
  • data data required for fill document (optional)
  • custom_helper own rendering helper (optional)

Provider#get prepare content by provided options:

  • relativefilepath relative path to the file (required)
  • data data required for fill document (optional)
  • custom_helper own rendering helper (optional)

Optional options override all optional options provided in Provider initialization

Provider return Content object

  • #source method provide source of document
  • #kind method return document kind (:orginal, :template, :unknown)

example:


|-- content
|   |-- documents
|   |   |-- cv.html.erb
|   |   |-- skills.html.erb
|   |   |-- additional_skils.html
|   |   |--base
|   |   |   |-- footer.html.erb

cv.html.erb source

Pawel N.<br/>
my skils: <%= render "skills.html" %>
<%= render "footer.html.erb" %>

skills.html.erb source

C#, Ruby, <%= render "additional_skils.html" %>

additional_skils.html source

Javascript</br>

base/footer.html.erb source

<%= 2+2 %>

output document should looks like:

Pawel N.<br/>
my skils: C#, Ruby, Javascript</br>
<%= 2+2 %>

when you want get orginal source of file you simple provide direct path to the file:

  • base/footer.html.erb returns <%= 2+2 %>
  • base/footer.html returns 4

Usage


content_provider = RenderingEngine::Provider.new(base_path)
content = content_provider.get(relative_file_path)
content.source #gets source of file (rendered or orginal)
content.kind     #return (:orginal, :template, :unknown)

example in controller can looks like this:

def show
  path = "#{params[:client]}/#{params[:path]}"
  data = params[:data]

  content = content_provider.get(path, data: data)
  return not_found if content.unknown?

  render text: content.source
end

def content_provider
  @content_provider ||= RenderingEngine::Provider.new(Rails.root.join('app/content'))
end

example with own content helper can looks like this:

class ContentCustomHelpers
  def initialize(opts={})
    @base_path = opts.fetch(:base_path)
    @data      = opts[:data]
  end

  def make_some_stuff
    "best line ever!"
  end

  def render(file_relative_path)
    file_path = File.join(base_path, file_relative_path)

    RenderingEngine::Content.new(file_path, data: data, custom_helper: self.class).source
  end

  attr_reader :data

  private

  attr_reader :base_path
end

usage

provider = RenderingEngine::Provider.new(Rails.root.join('app/content'), custom_helper: ContentCustomHelpers)
provider.get(path).source

or

provider = RenderingEngine::Provider.new(Rails.root.join('app/content'))
provider.get(path, custom_helper: ContentCustomHelpers).source