Teeplate

A Crystal library for rendering multiple template files.

CircleCI

Installation

Add this to your application's shard.yml:

dependencies:
  teeplate:
    github: mosop/teeplate

Supported Template Engines

Usage

require "teeplate"

Example

Let's make our crystal init template.

Template structure

Template file example

# shard.yml.ecr
name: <%= @file %>
version: 0.1.0

authors:
  - <%= @author %>

license: MIT

Template class definition

class CrystalInitTemplate < Teeplate::FileTree
  directory "#{__DIR__}/path/to/template"

  @file : String
  @class : String
  @author : String
  @year : Int32

  def initialize(@file, @class, @author, @year)
  end
end

Here we go!

template = CrystalInitTemplate.new("teeplate", "Teeplate", "mosop", 2016)
template.render "/path/to/output"

Output structure

Output example

# shard.yml
name: teeplate
version: 0.1.0

authors:
  - mosop

license: MIT

Overwriting

Forced

If a file already exists, Teeplate skips rendering the file. To force overwriting, set the :force option:

class Template < Teeplate::FileTree
  directory "/path/to/template"

  @face : String

  def initialize(@face)
  end
end

Template.new(":)").render("/path/to/output")
Template.new(":(").render("/path/to/output", force: true) # files may be overwritten
Template.new(":P").render("/path/to/output") # nothing happens

Interactive

If the :interactive option is true, Teeplate prompts us to select whether to overwrite an existing file.

shard.yml already exists...
overwrite(o)/keep(k)/diff(d)/overwrite all(a)/keep all(n) ?

Listing Rendered Files

You can print a list of rendered files. To enable it, set true to the :list option and call #render.

template = BrandNewCrystalInitTemplate.new("teeplate", "Teeplate", "mosop", 2017)
template.render "/path/to/overwrite", list: true

Output example:

modified  .gitignore
identical .travis.yml
new       Dockerfile
skipped   LICENSE
skipped   README.md
skipped   shard.yml
modified  spec/spec_helper.cr
skipped   spec/teeplate_spec.cr
skipped   src/teeplate/version.cr
skipped   src/teeplate.cr

You can colorize the output by the :color option.

template.render "/path/to/overwrite", list: true, color: true

Want-to-do

Release Notes