Some parts of this article have been written with AI. Be aware that AI can make mistakes.
Applies to : PowerDocs for Linux PowerDocs for Windows

Class `Templates`

The Templates class (accessed via context.templates) enables extensions to register reusable Markdown document templates and structured snippets.

Registered templates are automatically made available to the user in:

  • File Menu: File > New from template... (creates a new editor tab initialized with the template content).
  • Insert Menu: Insert > Template... (inserts or replaces selected text with the template content, with full Undo/Redo support).

Properties

Templates does not expose public properties.


Methods

registerTemplate(descriptor)

Registers a new document or fragment template.

Parameters

  • descriptor (object): A configuration object defining the template metadata and content generator.
Field Type Required Description
id string Yes Unique identifier for the template (e.g. 'adr-template').
title string Yes Human-readable title displayed in menus (e.g. 'Architecture Decision Record').
category string No Category name used for grouping (e.g. 'Architecture', 'Agile', 'API').
description string No Short description of the template purpose.
icon / iconName string No Icon identifier (e.g. 'DocumentTable', 'Code', 'Document').
content string No Static Markdown text for the template.
getContent function(env) No Dynamic callback invoked to generate the template text at runtime. Receives context.env.

Note

If both getContent and content are defined, getContent is executed first. If an error occurs, the static content is used as a fallback.


Examples

1. Dynamic Architecture Decision Record (ADR) Template

export function activate(context) {
    context.templates.registerTemplate({
        id: 'adr-template',
        title: 'Architecture Decision Record (ADR)',
        category: 'Architecture',
        description: 'Template for documenting critical architectural choices',
        icon: 'DocumentTable',
        getContent: (env) => {
            const now = new Date();
            const dateStr = now.toISOString().slice(0, 10);
            return `# ADR-${dateStr}: [Title of the Architecture Decision]

## Status
Proposed | **Accepted** | Superseded | Deprecated

## Context
[What problem or business context requires this architecture decision?]

## Decision
[What technical approach was chosen and why?]

## Consequences
### Positive
- Benefit 1
- Benefit 2

### Risks & Trade-offs
- Risk 1
`;
        }
    });
}

2. Agile User Story with Acceptance Criteria

export function activate(context) {
    context.templates.registerTemplate({
        id: 'user-story',
        title: 'User Story & Acceptance Criteria',
        category: 'Agile',
        description: 'Standard Given-When-Then user story format',
        icon: 'Document',
        content: `# US-[001] : [Feature Title]

**As a** [user persona],
**I want to** [perform an action],
**So that** [achieve business value].

---

## Acceptance Criteria
- [ ] **Scenario 1 : Happy Path**
  - **Given** [initial state],
  - **When** [action occurs],
  - **Then** [expected result].

- [ ] **Scenario 2 : Error Handling**
  - **Given** [invalid input],
  - **When** [submission is attempted],
  - **Then** [appropriate error message is displayed].
`
    });
}

3. REST API Endpoint Specification

export function activate(context) {
    context.templates.registerTemplate({
        id: 'api-endpoint',
        title: 'REST API Endpoint Spec',
        category: 'Backend',
        icon: 'Code',
        content: `# API Specification: [Service Name]

::: swagger GET /api/v1/resources
Retrieves a paginated list of resources matching the query criteria.
:::

## Query Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| \`page\` | integer | No | Page number (default: 1) |
| \`limit\` | integer | No | Items per page (default: 20) |

## Response (200 OK)
\`\`\`json
{
  "total": 42,
  "items": []
}
\`\`\`
`
    });
}