Module: Stellar::Erb

Defined in:
lib/stellar/erb.rb,
lib/stellar/erb/view.rb,
lib/stellar/erb/error.rb,
lib/stellar/erb/version.rb

Defined Under Namespace

Classes: Error, View

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.render(template_path, locals = {}) ⇒ String

Shortcut method for rendering templates This method provides a convenient interface to render ERB templates directly from the Stellar::Erb module without instantiating a View.

Examples:

Render a template with variables

Stellar::Erb.render("path/to/template.erb", name: "World")

Render a template without variables

Stellar::Erb.render("path/to/simple.erb")

Parameters:

  • template_path (String)

    The path to the ERB template file

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

    A hash of local variables to be made available in the template

Returns:

  • (String)

    The rendered template result

Raises:

  • (Stellar::Erb::Error)

    if an error occurs during template rendering

  • (Errno::ENOENT)

    if the template file doesn’t exist



30
31
32
# File 'lib/stellar/erb.rb', line 30

def self.render(template_path, locals = {})
  View.render(template_path, locals)
end

.render_string(str, locals = {}) ⇒ String

Renders an ERB template from a string instead of a file This is useful for template strings that are generated dynamically or stored in a database rather than in files.

Examples:

Render a string template with variables

Stellar::Erb.render_string("<p>Hello, <%= name %></p>", name: "World")

Render a string template without variables

Stellar::Erb.render_string("<p>Static content</p>")

Parameters:

  • str (String)

    The ERB template string to render

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

    A hash of local variables to be made available in the template

Returns:

  • (String)

    The rendered template result

Raises:



49
50
51
52
53
# File 'lib/stellar/erb.rb', line 49

def self.render_string(str, locals = {})
  v = View.new(nil, locals)
  v.template_content = str
  v.render()
end