DOCXLibWord documents for Ring

Complete example and mail merge

A complete document, and the mail merge template engine.

Complete Example#

Ring
load "docxlib.ring"

doc = new WordWriter()
doc.setTitle("Ring Language Performance Study")
doc.setAuthor("Mahmoud")
doc.setPageSize("a4")
doc.setTheme("Blue")

# Header / footer
doc.setFirstPageDifferent(true)
doc.setFirstPageHeader("")          # blank on cover
doc.setHeader("Ring Language Performance Study")
doc.showPageNumbers("right")

# Cover page
doc.addTitle("Ring Language Performance Study")
doc.addSubtitle("A Comparative Analysis — v1.11.0 Edition")
doc.addPageBreak()

# Front matter
doc.addTableOfContents("Table of Contents")
doc.addTableOfFigures("List of Figures")
doc.addTableOfTables("List of Tables")
doc.addPageBreak()

# Chapter 1
doc.addHeading("1. Introduction", 1)
doc.addParagraph("Ring is a multi-paradigm language designed for application development.", NULL)
doc.addFootnote("See ring-lang.github.io", "Ring official website.", NULL)
doc.addParagraph("", NULL)

doc.addHeading("1.1 Methodology", 2)
doc.addBulletList(["Benchmark suite", "Reference VM", "Metrics collected"])
doc.addParagraph("", NULL)

# Image + caption
doc.addImageCentered("chart.png", 12, 7)
doc.addFigureCaption("Benchmark results across five test configurations")
doc.addParagraph("", NULL)

# Table + caption
doc.addTableCaption("Raw timing data in milliseconds")
doc.addTable([
    ["Test Case",  "Ring", "Lang2", "Lang3"],
    ["Fibonacci",  "42",   "180",    "95"     ],
    ["Sort 10k",   "18",   "72",     "38"     ],
    ["File I/O",   "55",   "140",    "60"     ]
], [
    :headerRow      = true,
    :borderStyle    = "single",
    :headerBgColor  = "1E6BB5",
    :evenRowBgColor = "DEEAF1"
])

doc.save("report.docx")

Mail Merge Template Engine#

A full template-based mail merge engine. Define a template once with setMergeTemplate(), then call mergeRecord() or mergeAll() to produce personalised documents for any number of records.

API#

MethodDescription
setMergeTemplate(template)Register a template definition (list of items)
clearMergeTemplate()Remove the registered template
mergeRecord(data)Render the template once, substituting data into all {{FIELD}} tokens
mergeAll(dataList, sep)Render for every record in dataList; insert separator between records

mergeAll separator values: "pagebreak" (default) · "emptyline" · "line" · "none"

Template Item Types#

A template is a Ring list. Each item is either a plain string or an associative list:

Item typeDescription
"Dear {{FirstName}},"Plain string — rendered as a paragraph; {{FIELD}} tokens filled
[:type="heading", :level=N, :text="{{Field}} Report"]Heading at level 1–6
[:type="paragraph", :text="...", :options=[...]]Paragraph with full formatting options
[:type="table", :data=[...], :options=[...]]Table — {{FIELD}} tokens in cells are filled
[:type="pagebreak"]Page break
[:type="emptyline"]Empty paragraph

{{FIELD}} tokens work in all text fields including table cells. Unknown fields are left as-is.

Data Record Format#

A data record is a Ring associative list:

Ring
data = [
    :FirstName = "Alice",
    :LastName  = "Smith",
    :Score     = "94"
]

mergeAll() takes a list of such records:

Ring
dataList = [
    [:Name = "Alice", :Score = "94"],
    [:Name = "Bob",   :Score = "78"]
]

Code Examples#

Simple letter — three recipients, page break between each:

Ring
doc.setMergeTemplate([
    [:type = "paragraph", :text = "{{Date}}", :options = [:align = "right"]],
    [:type = "emptyline"],
    "Dear {{Salutation}} {{LastName}},",
    [:type = "emptyline"],
    [:type = "paragraph",
     :text = "Your application to {{ProgramName}} has been accepted. " +
             "Your enrolment number is {{EnrolmentNumber}}."],
    [:type = "emptyline"],
    [:type = "paragraph", :text = "Yours sincerely,"],
    [:type = "paragraph", :text = "Mahmoud", :options = [:bold = true]]
])

recipients = [
    [:Date = "1 March 2026", :Salutation = "Ms", :LastName = "Benali",
     :ProgramName = "Ring MSc", :EnrolmentNumber = "MSC-2026-001"],
    [:Date = "1 March 2026", :Salutation = "Mr", :LastName = "Mendes",
     :ProgramName = "Ring MSc", :EnrolmentNumber = "MSC-2026-002"]
]

doc.mergeAll(recipients, "pagebreak")
doc.clearMergeTemplate()

Invoice template with an embedded table:

Ring
doc.setMergeTemplate([
    [:type = "heading", :level = 1, :text = "INVOICE #{{InvoiceNum}}"],
    [:type = "table",
     :data = [
         ["Bill To",  "{{ClientName}}",  "Date",    "{{InvoiceDate}}"],
         ["Address",  "{{Address}}",     "Due",     "{{DueDate}}"]
     ],
     :options = [:borderStyle = "single", :colWidths = [3.5, 5.0, 2.5, 5.0]]],
    [:type = "emptyline"],
    [:type = "paragraph",
     :text = "Total Due: {{Total}}", :options = [:bold = true, :align = "right"]]
])

doc.mergeAll(invoices, "pagebreak")
DOCXLib - Word documents for Ring, made with ringdoc.