Skip to content
/ sablon Public

Docx templater with image integration compabilty

License

Notifications You must be signed in to change notification settings

kubido/sablon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sablon

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 it easy to create your templates.

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

Installation

Add this line to your application's Gemfile:

gem 'sablon'

Usage

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

Writing Templates

Sablon templates are normal word documents (.docx) sprinkled with MergeFields to perform operations. The following section will use 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:

«=title»

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

«=post.title»

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.

Conditionals

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

«technologies:if»
    ... arbitrary document markup ...
«technologies:endIf»

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:

«body:if(present?)»
    ... arbitrary document markup ...
«body:endIf»

Loops

Loops repeat parts of the document.

«technologies:each(technology)»
    ... arbitrary document markup ...
    ... use `technology` to refer to the current item ...
«technologies:endEach»

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.

Nesting

It is possible to nest loops and conditionals.

Executable

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>

Have a look at this test for an example.

Examples

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

Contributing

  1. Fork it ( https://github.com/[my-github-username]/sablon/fork )
  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

Inspiration

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

About

Docx templater with image integration compabilty

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages