Skip to content

Use the power of liquid template language in your documents

License

Notifications You must be signed in to change notification settings

railscard/liquidoc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Liquidoc

Bring the power of Liquid template language to your documents! Liquidoc gives you ability to easily create documents from templates. Only .txt and .docx templates are currently supported.

Liquidoc is super-easy to use so even your customers can create their own templates.

Liquidoc demo

Installation

Add this line to your application's Gemfile:

gem 'liquidoc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install liquidoc

Usage

To get started you need to create a template:

require 'liquidoc'

template = Liquidoc.template('/path/to/file')

Then you need a context, it is just a plain ruby hash:

context = { "name" => "Max", "hobby" => "Music" }

Now you can create the final document:

template.render_to_file("/tmp/document_demo.docx", context)

Rails

config/initializers/mime_types.rb

Mime::Type.register 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', :docx

app/drops/invoice_drop.rb

class InvoiceDrop < Liquid::Drop
  def initialize(invoice)
    @invoice = invoice
  end

  def date
    @invoice.date
  end

  # And you can just proxy every method call directly to invoice (may be dangerous!)
  def before_method(method_name)
    if @invoice.respond_to?(method_name)
      @invoice.public_send(method_name)
    else
      "Unknown method: #{method_name}"
    end
  end
end

app/controllers/invoices_controller.rb

class InvoiceController < ApplicationController
  def show
    @invoice = Invoice.find(params[:id])

    respond_to do |format|
      format.html
      format.docx {
        template = Liquidoc.template(@invoice.template)
        content  = { 'invoice' => InvoiceDrop.new(@invoice) }
        send_data template.render_to_string(content), filename: 'invoice.docx'
      }
    end
  end
end

Contributing

  1. Fork it ( http://github.com/railscard/liquidoc/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 new Pull Request

Special thanks to Shopify!

About

Use the power of liquid template language in your documents

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages