Developer template editor

An editor for developer templates: it writes the TOML header from a parameter list, helps with the Jinja body, checks both while you type and previews the SQL. The file format is in Writing your own templates.

A template tab opens no session. The preview uses the version of the server selected in the object tree; Test reads its choice lists over the tree's session.

The template manager

Tools → Developer templates… (command template_manager, no default key).

The template manager
The templates folder: sections and their templates.

Unlike the F7 picker, the manager lists empty sections and every file: a version variant has its own row, ending with its folder ([12_plus]).

ButtonKey (in the list)Action
EditEnter, E, double clickOpens the file in a template tab; a file already open is activated.
NewNA new template for the section the cursor is in.
SectionTA new, empty section.
CopyCCopies the template to a new file name in the same section.
RenameRRenames the template's file or the section's directory.
DeleteDDeletes the template file or the empty section.
SearchAlt+S, SMatches name, description and file name.
CloseEsc, L

Sections

Copy, rename, delete

A new template, step by step

  1. In the manager, N on the target section.
    A new template tab
    The file bar names the section and version folder the first save will propose.
  2. Enter on Name: name and description.
  3. Insert for each parameter (parameter dialog).
  4. Tab to the body; write the SQL. F7 inserts parameters, constructs and filters.
  5. Ctrl+S saves; F5 tests the form.

The template tab

A template tab
Header (left), body, preview, status row.

Below about 70 columns the header is placed above the body.

Moving between the parts

KeyAction
Alt+Down / Alt+UpNext / previous part: header → body → preview.
Tab / Shift+TabThe same from the header list and the preview. In the body and the raw header Tab indents.
Ctrl+RHides / shows the preview.
Alt+Shift+Up / Alt+Shift+DownPreview smaller / larger (or drag the Preview bar).

The keys of the tab

Ctrl+S save, F5 test, F7 helper menu, Ctrl+T list ↔ raw header. F5 and F7 differ from a query tab's. Full list: below.

The header list

KeyAction
Enter, double clickEdit: name and description, or the parameter.
InsertAdd a parameter.
DeleteDelete the parameter (confirmed; the list has no undo).
Ctrl+Up / Ctrl+Down, Shift+Up / Shift+DownMove the parameter: the order of the [[param]] tables and of the form's fields.
F2Context menu.

Renaming a parameter does not rewrite the body; the status row shows Not declared: old · Not used: new.

The parameter dialog

The parameter dialog
One [[param]] table; the fields are its keys.

The types

text, identifier, number, bool, choice, sql, list — see Parameter types. For sql, Takes the free rows of the form is fill = true.

A choice and its values

A choice from database objects
Database objects (source): one of the built-in catalog queries.
A choice from a query
Query (query): one SELECT / WITH / VALUES / TABLE statement, run read-only; the first column gives the values.

Only a source is preset from the tree's selection and yields schema-qualified objects ({{ t|ident }}, {{ t.schema }}, {{ t.name }}); a query yields strings.

A list and its fields

A list parameter
A list parameter: its fields are the [[param.fields]] tables.
The dialog of a field
A field is declared like a parameter, without list and fill.

Buttons: Alt+A add, Alt+E edit, Alt+D delete, Alt+U / Alt+W up / down. Field changes are applied only when the list's dialog is confirmed.

The body and its helper menu

The helper menu
F7.

The body is a Jinja template. F7 offers the insertions below, plus Add variant for version…, Raw header, Test… and Save as…. Each insertion is one undo step.

Insert parameter

Insert parameter
The parameters, then the context variables server_version and database.
ParameterInserted text
text, number, sql, choice with options or query{{ comment }}
identifier, choice with a source{{ name|ident }}
bool{% if with_audit %} … {% endif %}
listA scaffold loop with every field and a comma between rows:
{% for row in columns %}
    {{ row.col_name|ident }} {{ row.col_type }} {{ row.not_null }} {{ row.primary_key }} {{ row.col_default }}{% if not loop.last %},{% endif %}

{% endfor %}

The empty line is needed: with trim_blocks the tag at the end of the line consumes the line break.

Insert construct

Insert construct
The constructs.

if, if / else, for, the comma between rows ({% if not loop.last %},{% endif %}), {{ }}, {# #}, {% if server_version >= 170000 %}, and one {% if has("…") %} entry per feature of pgtc's feature table, with the PostgreSQL version that introduced it. With a selection, the construct is wrapped around it.

A construct around the selection
if … endif wrapped around a selected line.

Insert filter

Insert filter
The filters.

Inside a {{ … }} the filter is appended to the expression; a selected word becomes {{ word|filter }}; elsewhere a new {{ |filter }} is inserted.

FilterResult
identIdentifier, quoted where needed. A source value: schema.name, each part quoted; a routine keeps its argument list, a trigger its ON schema.table.
qtLiteralString literal ('O''Brien'); E'…' when the value has a backslash; numbers and booleans unquoted; undefined → NULL.
qtTypeIdentType name as an identifier; multi-word names (double precision) stay unquoted, [] is kept.
indent(4, true), default(""), upper, lower, trim, replace("a", "b"), join(", "), lengthAs in Jinja. Every other minijinja filter works when typed.

Make parameter from selection

Select text in the body, F7, Make parameter from selection…: the parameter dialog opens with the selection as Default; on OK the parameter is appended and its expression replaces the selection, so the rendered SQL is unchanged. A multi-line selection becomes a sql parameter. Not available in the raw header view.

The parameter in place
{{ comment }} in place of the text.

Autocomplete in the body

Same keys as in a query tab (Ctrl+Space, Alt+/). The candidates come from the template, not from a catalog. The popup opens by itself inside a tag on the first letter of a name, after ., | and has("; in the SQL after the third letter.

Autocomplete in the SQL of the body
In the SQL: parameters first (in header order), then key words.

In the SQL, accepting a parameter inserts what Insert parameter inserts ({{ schema|ident }}, an if block, a loop). Inside a tag:

AfterCandidates
{{ , {% if Parameters (bare names), loop variables and loop, {% set %} variables, server_version, database, has(", and not, and, or, in, is, true, false, none.
row.The fields of the list the loop iterates.
loop.last, first, index, index0, length.
table.For a source value: schema, name; args for routines, table for triggers.
|The filters; indent, default, replace, join with their arguments.
{% if, elif, else, endif, for, endfor, set.
has("The feature names, with the version that introduced each.
The fields of a list's rows
After c. in {% for c in columns %}.

Preview and checks

The preview renders the body after every change, with sample values:

ParameterSample value
with a defaultThe default.
required, no defaultIts own name; 1 for a number; the object schema.name for a schema-qualified source.
not required, no defaultEmpty.
boolIts default, else false.
listOne row; each field its default, else its name.
The checks in the status row
{{ length }} used, code_length declared: the undefined variable renders empty.

The checks are static: the header is validated as the file parser does, the body is compiled by minijinja, and its variables are compared with the declarations.

Status rowMeaning
Not declared: …Warning. Variables that are neither parameters nor context variables; they render as an empty string.
Not used: …Warning. Parameters the body never references.
Header: …Error. An invalid declaration; in the raw header also a TOML error or an unknown key.
Body, line N: …Error. A Jinja syntax error.
Preview: …Error. The body compiles but does not render with the sample values: an unknown filter, an unknown feature name in has("…"). A branch the sample values do not reach is not checked.

A template with an error is not saved and not tested.

A Jinja error
{% endfi %}: the status row and the preview name the line.

Enter in the preview jumps to the line of a Jinja syntax error; a refused Ctrl+S or F5 does the same.

The server version of the preview

Status rowWhen
Preview: PostgreSQL 17The row selected in the object tree belongs to a connected server: its server_version_num.
Preview: PostgreSQL 16 (assumed)Otherwise. A file in 16_plus: 160000. A file in default: the newest version in pgtc's feature table, so every has("…") is true.

The raw header

The raw header
Ctrl+T: the header as TOML text.

Test: the template in its form

Testing the template
F5: the F7 form of the tab's current text.

Saving

Ctrl+S. The file is replaced atomically, in the background.

Save template
The first save, and Save as….

The built-in templates

Saving a template of pgtc
Saving a built-in template.

A file is built-in if its path below the templates folder equals that of an embedded template (default/Tables/table.pgtemplate). Settings → Install templates overwrites those files, so pgtc asks:

Closing a tab with changes

As for a query tab, governed by the same setting (Ask to save query tabs when closing). If the save is refused, the tab stays open.

Variants for server versions

A variant is a file with the same section and file name in a version folder N_plus; F7 picks the folder with the highest N ≤ the server's major version, else default. For small differences a version or feature test is enough.

Keys of the template tab

KeyContextActionCommand
Ctrl+STabSavesave_file
F5TabTesttemplate_test
F7TabHelper menutemplate_helpers
Ctrl+TTabParameter list / raw headertemplate_raw_header
Alt+Down / Alt+UpTabNext / previous partfocus_lower, focus_upper
Ctrl+RTabHide / show the previewtoggle_results
Alt+Shift+Up / Alt+Shift+DownTabPreview smaller / largerresults_smaller, results_larger
Tab / Shift+TabHeader list, previewNext / previous partnext_field, prev_field
EnterHeader listEdittemplate_edit_param
InsertHeader listAdd a parametertemplate_add_param
DeleteHeader listDelete the parametertemplate_delete_param
Ctrl+Up, Shift+UpHeader listMove the parameter uptemplate_param_up
Ctrl+Down, Shift+DownHeader listMove the parameter downtemplate_param_down
F2Header listContext menucontext_menu
EnterPreviewGo to the body's syntax error
Ctrl+Space, Alt+/BodyAutocompletecomplete

The template bindings are in the [template] table of keymap.toml. Commands without a default key: template_insert_param, template_insert_construct, template_insert_filter, template_make_param, template_add_variant, template_manager.