/mruby-erb

Direct port of ERB from CRuby -> MRuby

Primary LanguageRuby

mruby-erb

ERB, modified (slightly) for MRuby.

Modifications

  • Anywhere CRuby used binding arguments, mruby-erb accepts an object.
  • The template is instance_evaled on the object, instead of being evaled in the context of a binding.
  • No ERB::Util (CGI stuff... you don't need it).
  • No encodings support (MRuby has limited support for this in general).

Dependencies

Depends on some regexp implementation. By default, mruby-regexp-pcre is used. You can override this choice by setting the MRUBY_REGEXP_GEM environmnet variable. This should hopefully eliminate the need to fork this gem for each regexp implementation. (See: mrbgem.rake)

API

# Compile an ERB template.
# - safe_level is here for API compatability only, and is not used.
# - The rest is exactly what you're used to from CRuby
template = ERB.new(str, safe_level[=nil], trim_mode[=nil], eoutvar[='_erbout'])

# Execute a template and return the resulting string.
# Accepts an object, rather than a binding.
template.result(obj[=nil])

# Define `methodname` as instance method of `mod` from compiled ruby source.
template.def_method(mod, methodname, fname[='(ERB)'])

# Define module with methodname as an instance method from compiled ruby source.
template.def_module(methodname[='erb'])

# Define class with methodname as an instance method from compiled ruby source.
template.def_class(superclass[=Object], methodname[='result'])
## Example:
# tmp = "<%= a %> <%= b %>"
# erb = ERB.new(tmp)
# Renderer = erb.def_class(Object, 'render(a, b)')
# renderer = Renderer.new
# renderer.render(1, 2)
#  => "1 2"