Class: Malt::Engine::Erb

Inherits:
Abstract show all
Defined in:
lib/malt/engines/erb.rb

Overview

ERB template implementation.

http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html

ERB templates accept two options. safe sets the safe mode for rendering the template and trim is a weird string that controls a few rendering options –it can be '%' and/or '>' or '<>'.

Instance Attribute Summary

Attributes inherited from Abstract

#settings

Instance Method Summary collapse

Methods inherited from Abstract

#cache?, default, #initialize, #prepare_engine, register, type

Constructor Details

This class inherits a constructor from Malt::Engine::Abstract

Instance Method Details

#create_engine(params = {}) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/malt/engines/erb.rb', line 64

def create_engine(params={})
  text = parameters(params, :text)

  opts = engine_options(params)
  safe = opts[:safe]
  trim = opts[:trim]

  cached(text,safe,trim) do
    ::ERB.new(text, safe, trim)
  end
end

#render(params = {}, &content) ⇒ String

Render ERB template.

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :text (String)

    Text of document.

  • :file (String)

    The file name where text was read (or nil).

  • :scope (Object, Binding)

    Scope from within which to render, serves as data source for template interpolation.

  • :locals (Object, Binding)

    Direct key/value data for template interpolation.

  • :safe (Boolean)

    Run in separate thread.

  • :trim (String)

    Trim mode, can be either of the following:

    a) `%`  enables Ruby code processing for lines beginning with `%`.
    b) `<>` omit newline for lines starting with `<%` and ending in `%>`.
    c) `>`  omit newline for lines ending in `%>`.
    
  • :precompile (Boolean) — default: true

    Precompile the ERB template. Default is `true`. Note that `yield` currently does work with non-compiled tempaltes.

Returns:

  • (String)

    Rendered output.


48
49
50
51
52
53
54
55
56
# File 'lib/malt/engines/erb.rb', line 48

def render(params={}, &content)
  text, file, scope, locals = parameters(params, :text, :file, :scope, :locals)

  #if settings[:compile] == true
  #else
    bind = make_binding(scope, locals, &content)
    prepare_engine(params).result(bind)
  #end
end