Ruby Document Template Processor based on docx templates and Mail Merge fields.
Clone or download
Pull request Compare This branch is 4 commits ahead, 49 commits behind senny:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Gem Version Build Status

Is a document template processor for Word docx files. It leverages Word's built-in formatting and layouting capabilities to make template creation easy and efficient.

Note: Sablon is still in early development. Please report if you encounter any issues along the way.


Add this line to your application's Gemfile:

gem 'sablon'


require "sablon"
template = Sablon.template(File.expand_path("~/Desktop/template.docx"))
context = {
  title: "Fabulous Document",
  technologies: ["Ruby", "HTML", "ODF"]
template.render_to_file File.expand_path("~/Desktop/output.docx"), context

Writing Templates

Sablon templates are normal Word documents (.docx) sprinkled with MailMerge fields to perform operations. The following section uses the notation «=title» to refer to Word MailMerge fields.

Content Insertion

The most basic operation is to insert content. The contents of a context variable can be inserted using a field like:


It's also possible to call a method on a context object using:


NOTE: The dot operator can also be used to perform a hash lookup. This means that it's not possible to call methods on a hash instance. Sablon will always try to make a lookup instead.

This works for chained method calls and nested hash lookup as well:


Generally Sablon tries to reuse the formatting defined in the template. However, there are situations where more fine grained control is needed. Imagine you need to insert a body of text containing different formats. If you can't decide the format ahead of processing time (in the template) you can insert WordProcessingML directly.

It's enough to use a simply insertion operation in the template:


To insert WordProcessingML prepare the context accordingly:

word_processing_ml = <<-XML.gsub("\n", "")
<w:r w:rsidRPr="00B97C39">
<w:b />
<w:t>this is bold text</w:t>

context = {
  long_description: Sablon.content(:word_ml, word_processing_ml)
template.render_to_file File.expand_path("~/Desktop/output.docx"), context

IMPORTANT: This feature is very much experimental. Currently, the insertion will replace the containing paragraph. This means that other content in the same paragraph is discarded.

HTML [experimental]

Similar to WordProcessingML it's possible to use html as input while processing the tempalte. You don't need to modify your templates, a simple insertion operation is sufficient:


To use HTML insertion prepare the context like so:

html_body = <<-HTML
<div>This text can contain <em>additional formatting</em>
according to the <strong>HTML</strong> specification.</div>
context = {
  article: { html_body: Sablon.content(:html, html_body) }
template.render_to_file File.expand_path("~/Desktop/output.docx"), context

Currently HTML insertion is very limited and strongly focused on the HTML generated by Trix editor.

IMPORTANT: This feature is very much experimental. Currently, the insertion will replace the containing paragraph. This means that other content in the same paragraph is discarded.


Sablon can render parts of the template conditonally based on the value of a context variable. Conditional fields are inserted around the content.

    ... arbitrary document markup ...

This will render the enclosed markup only if the expression is truthy. Note that nil, false and [] are considered falsy. Everything else is truthy.

For more complex conditionals you can use a predicate like so:

    ... arbitrary document markup ...


Loops repeat parts of the document.

    ... arbitrary document markup ...
    ... use `technology` to refer to the current item ...

Loops can be used to repeat table rows or list enumerations. The fields need to be placed in within table cells or enumeration items enclosing the rows or items to repeat. Have a look at the example template for more details.


It is possible to nest loops and conditionals.


Sometimes it's necessary to include markup in the template that should not be visible in the rendered output. For example when defining sample numbering styles for HTML insertion.

    ... arbitrary document markup ...


The sablon executable can be used to process templates on the command-line. The usage is as follows:

cat <context path>.json | sablon <template path> <output path>

If no <output path> is given, the document will be printed to stdout.

Have a look at this test for examples.


Using a Ruby script

There is a sample template in the repository, which illustrates the functionality of sablon:

Sablon Template

Processing this template with some sample data yields the following output document. For more details, check out this test case.

Sablon Output

Using the sablon executable

The executable test showcases the sablon executable.

The template

Sablon Output

is rendered using a json context to provide the data. Following is the resulting output:

Sablon Output


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request


These projects address a similar goal and inspired the work on Sablon: