Sha256: 2eca302bacf9d12ea349c5fc7cb5c2593a92b09706f1d12e4dafebe091156015

Contents?: true

Size: 838 Bytes

Versions: 3

Compression:

Stored size: 838 Bytes

Contents

class ERBContext

  def initialize(hash)
    hash.each_pair do |key, value|
      instance_variable_set('@' + key.to_s, value)
    end
  end

  def get_binding
    binding
  end

end

class String

  # Converts the string using eRuby
  def eruby(params={})
    params[:eruby_engine] == :erubis ? erubis(params) : erb(params)
  end

  # Converts the string using Erubis
  def erubis(params={})
    nanoc_require 'erubis'
    Erubis::Eruby.new(self).evaluate(params[:assigns] || {})
  end

  # Converts the string using ERB
  def erb(params={})
    nanoc_require 'erb'
    ERB.new(self).result(ERBContext.new(params[:assigns] || {}).get_binding)
  end

end

register_filter 'eruby' do |page, pages, config|
  assigns = { :page => page, :pages => pages }
  page.content.eruby(:assigns => assigns, :eruby_engine => config[:eruby_engine])
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nanoc-1.6 lib/nanoc/filters/eruby_filter.rb
nanoc-1.6.1 lib/nanoc/filters/eruby_filter.rb
nanoc-1.6.2 lib/nanoc/filters/eruby_filter.rb