Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rendering A Template

We can render a template to a file or to stdout with the command sketch render.

From A Configuration File

A template can be defined as simple text in a config file, inside the templates map. In this case, you can refer to this template by using its map key. This is the easiest method, but it also means slightly worse IDE integration for things like snippets and syntax highlighting.

templates_dir: templates

vars:
  location: Isengard
  greeting: hello
  general: kenobi
  meal: breakfast

templates:
  hobbits: "they're taking the hobbits to {{ location }}!"
  breakfast: "what about second {{ meal }}?"

Command:

sketch render --id hobbits tests/output/custom_templates/from_template_id.txt

From templates_dir

The best method is to use a file inside templates_dir. With this method, the template's ID becomes the relative path from templates_dir. This method provides better IDE integration if you have set up snippets or syntax highlighting for jinja files (Tera, the templating engine used by sketch is based on jinja).

Example

Tree structure of templates_dir:

├── example.j2
└── subdir
    ├── nested
    │   └── more_nested_file.j2
    └── nested_file.j2

Command:

sketch render --template subdir/nested_file.j2 tests/output/custom_templates/from_template_file.txt

ℹ️ You can also use the -f flag to render a template from any file, even outside templates_dir.

ℹ️ --id and --template are aliases for one another, and they can be used to refer to a file inside templates_dir, or to a literal template in the config file.

From Literal Definition

Alternatively, a template can also be defined directly within a command:

sketch render --content "they're taking the hobbits to {{ location }}!" tests/output/custom_templates/from_literal.txt