Module: TemplateRenderer

Included in:
Generator
Defined in:
lib/generators/template_renderer.rb,
lib/generators/template_renderer/partial_cache.rb,
lib/generators/template_renderer/template_error.rb,
lib/generators/template_renderer/partial_resolver.rb

Overview

Template rendering module for Ruby Raider generators

Provides a clean partial() helper for including ERB templates with:

  • Automatic caching of compiled ERB objects (10x performance improvement)

  • Context-aware path resolution (relative to caller, then all source_paths)

  • Helpful error messages when partials are missing

  • Flexible whitespace handling (trim_mode, strip options)

Usage in templates:

<%= partial('screenshot') %>                    # Default: trim_mode: '-', no strip
<%= partial('screenshot', strip: true) %>       # With .strip!
<%= partial('screenshot', trim: false) %>       # No trim_mode
<%= partial('driver_config', trim_mode: '<>') %> # Custom trim_mode

The partial() method automatically has access to all generator instance variables and methods (cucumber?, mobile?, selenium?, etc.) through binding.

Defined Under Namespace

Modules: ClassMethods Classes: PartialCache, PartialResolver, TemplateError, TemplateNotFoundError, TemplateRenderError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Module hook for including in classes



67
68
69
# File 'lib/generators/template_renderer.rb', line 67

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#partial(name, options = {}) ⇒ String

Render a partial template with caching and smart path resolution

Examples:

Basic usage

<%= partial('screenshot') %>

With strip

<%= partial('screenshot', strip: true) %>

No trim mode

<%= partial('quit_driver', trim: false) %>

Parameters:

  • name (String)

    Partial name without .tt extension (e.g., ‘screenshot’)

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

    Rendering options

Options Hash (options):

  • :trim_mode (String, nil)

    ERB trim mode (default: ‘-’)

    • ‘-’ : Trim lines ending with -%>

    • ‘<>’ : Omit newlines for lines starting with <% and ending with %>

    • nil : No trimming

  • :strip (Boolean)

    Whether to call .strip! on result (default: false)

  • :trim (Boolean)

    Whether to use trim_mode (default: true)

Returns:

  • (String)

    Rendered template content

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/template_renderer.rb', line 48

def partial(name, options = {})
  # Default options
  options = {
    trim_mode: '-',
    strip: false,
    trim: true
  }.merge(options)

  # Handle trim: false by setting trim_mode to nil
  options[:trim_mode] = nil if options[:trim] == false

  # Render the partial through the cache
  result = self.class.template_renderer.render_partial(name, binding, options)

  # Apply strip if requested
  options[:strip] ? result.strip : result
end