Class: Uniword::Template::TemplateRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/template/template_renderer.rb

Overview

Renders templates by processing markers and filling with data.

Takes a template and data, processes all markers in order, and produces a filled document. Handles variable substitution, loops, and conditionals.

Responsibility: Template rendering only Single Responsibility Principle: Does NOT parse or validate

Examples:

Render simple template

renderer = TemplateRenderer.new(template, data, {})
document = renderer.render

Instance Method Summary collapse

Constructor Details

#initialize(template, data, additional_context = {}) ⇒ TemplateRenderer

Initialize renderer

Parameters:

  • template (Template)

    Template to render

  • data (Hash, Object)

    Data to fill template

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

    Additional context



26
27
28
29
30
31
# File 'lib/uniword/template/template_renderer.rb', line 26

def initialize(template, data, additional_context = {})
  @template = template
  @data = data
  @additional_context = additional_context
  @context = TemplateContext.new(data, additional_context)
end

Instance Method Details

#renderDocument

Render template with data

Creates a new document by cloning the template document and processing all markers.

Returns:

  • (Document)

    Rendered document



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/uniword/template/template_renderer.rb', line 39

def render
  # Clone template document
  output = clone_document(@template.document)

  # Process markers
  process_markers(output)

  # Remove template comments
  remove_template_comments(output)

  output
end