Ruby document template processor based on docx templates and Mail Merge fields
open sourceMITself-hostablefree
Sablon is a Ruby gem that fills Word (.docx) templates: the template is an ordinary Word document with MailMerge fields such as «=title», and Sablon replaces them with values from a Ruby hash or objects. Fields also drive conditionals (if, elsif, else), loops that repeat table rows or list items, nesting and comments, and you can insert raw WordprocessingML, HTML (converted to Word paragraphs, tables and lists) or images. It ships a sablon executable that renders a template from JSON. Yves Senn started it in 2014; it's MIT-licensed and still released (0.4.4 in November 2026).
Problems it solves
- fill a Word template from my Rails app and keep all the styling the business team set up in Word
- repeat a row of a Word table for every invoice line in a Ruby report
- show or hide a section of a .docx depending on a value, generated from Ruby
- turn HTML from our rich text field into real Word paragraphs and tables inside a generated document
- put a different photo into each generated Word document from a Ruby script
- templates made with Word's own mail merge fields instead of curly-brace tags people mistype
- render a .docx from a JSON file on the command line without writing a program
- an MIT-licensed Ruby gem for Word document generation with no paid modules
Features
- MailMerge-field templates (
«=field») made in Word - method calls and nested hash lookups (
«=buyer.address.street») - conditionals with predicates, elsif and else
- loops that repeat table rows or list items, with nesting
- insert raw WordprocessingML, inline or as paragraphs
- HTML insertion converted to Word paragraphs, tables and lists, with configurable tag and CSS conversion
- images from files, URLs or IO objects, sized by the placeholder or explicitly (beta)
- template comments that don't appear in the output
sablonexecutable that renders a template from JSON
Limitations
- Doesn't convert every CSS style when inserting HTML: only a small subset of inline styles has a Word equivalent1
Capabilities
| Has a command line tool | plan not known |
|---|
Alternatives
The same job
These do what Sablon does.
- docxtpl Use a docx as a jinja2 template
- Docmosis API-first, template-based document generation
- docxtemplater Powerful Word, PowerPoint and Excel generation
- Carbone Generates PDF, DOCX and other documents from JSON data and Office templates
- docx-templates Template-based docx report creation for both Node and the browser
- easy-template-x Generate docx documents from templates, in Node or in the browser
