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

Templating Presets

Rendering a single template is for cases when we need a simpler or more flexible setup. But things become more interesting with templating presets, where different kinds of templates can be aggregated and used in order to define easily reproducible project structures.

Templating presets can be rendered with the render-preset command, or as part of another preset, such as a git repo preset.

A templating preset contains an optional context (which overrides the global context), and one or many of these items:

1. Individual Templates

  • Individual templates, which provide a manually controlled output path and their own local context

Example

templating_presets:

  lotr:
    # Group context, lower priority
    context: {}
    templates:
      # Selecting individual templates
      - template: hobbits
        output: hobbits.txt
        # Individual context, higher priority
        context: {}
      - template: breakfast
        output: subdir/breakfast.txt

Command:

sketch render-preset lotr

Tree output:

├── hobbits.txt
└── subdir
    └── breakfast.txt

2. Template Directory

  • A path to a directory inside templates_dir, where all templates will be recursively rendered in the output directory, with the same file tree structure

ℹ️ Any template files that end with the .j2, .jinja or .jinja2 extensions will have them automatically removed. So myfile.json.j2 will just become myfile.json.

Example

Let's say that this is our templates_dir:

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

And we define this preset, which is meant to reproduce the entire file structure of subdir in the target directory.

templating_presets:
  structured:
    # Selecting all the files inside this directory
    # and its descendants
    templates:
      - dir: subdir
        # Glob patterns to exclude certain templates
        exclude:
          - "some/glob/pattern/*"

Command:

sketch render-preset structured

Tree output:

├── nested
│   └── more_nested_file
└── nested_file

3. Remote Template

  • A special kind of template which points to a git repository. Every file inside of it will be rendered in the output directory.
templating_presets:
  remote:
    templates:
      - repo: https://github.com/Rick-Phoenix/sketch-remote-preset-example
        # Glob patterns to exclude
        exclude: [.gitignore]

Example

We start from this basic example

Command:

sketch --set 'continuation="gp2 engine... gp2!"' render-preset remote

Tree output:

├── some_file
└── subdir
    └── nested
        └── nested_file

File output for some_file:

Roses are red, violets are blue, gp2 engine... gp2!

Extending Templating Presets

Templating presets are extensible. When a preset is being extended, its templates will be added to the receiving preset, and the two context maps will be merged, with the new context overwriting the previous context in case of conflicting variables.

  extended:
    extends_presets:
      - lotr