Working with Templates
Generate code from predefined templates stored in .knowns/templates/.
Announce at start: "I'm using the knowns.template skill to work with templates."
Core principle: USE TEMPLATES FOR CONSISTENT CODE GENERATION.
The Process
Step 1: List Available Templates
{{#if mcp}}
json
mcp__knowns__list_templates({})
{{else}}
bash
knowns template list
{{/if}}
Step 2: Get Template Details
{{#if mcp}}
json
mcp__knowns__get_template({ "name": "<template-name>" })
{{else}}
bash
knowns template info <template-name>
{{/if}}
Check:
- •Required variables (prompts)
- •Linked documentation (
doc:) - •Files that will be generated
Step 3: Read Linked Documentation
If template has a doc: field, read it first:
{{#if mcp}}
json
mcp__knowns__get_doc({ "path": "<doc-path>", "smart": true })
{{else}}
bash
knowns doc "<doc-path>" --plain
{{/if}}
Step 4: Run Template
{{#if mcp}}
json
// Dry run first (preview)
mcp__knowns__run_template({
"name": "<template-name>",
"variables": { "name": "MyComponent", "type": "page" },
"dryRun": true
})
// Then run for real
mcp__knowns__run_template({
"name": "<template-name>",
"variables": { "name": "MyComponent", "type": "page" },
"dryRun": false
})
{{else}}
bash
# Dry run (preview) knowns template run <template-name> --name "MyComponent" --dry-run # Run for real knowns template run <template-name> --name "MyComponent"
{{/if}}
Step 5: Create New Template
{{#if mcp}}
json
mcp__knowns__create_template({
"name": "<template-name>",
"description": "Template description",
"doc": "patterns/<related-doc>" // Optional: link to documentation
})
{{else}}
bash
knowns template create <template-name>
{{/if}}
This creates:
code
.knowns/templates/<template-name>/ ├── _template.yaml # Config └── example.ts.hbs # Example file
Template Config (_template.yaml)
yaml
name: react-component
description: Create a React component with tests
doc: patterns/react-component # Link to documentation
prompts:
- name: name
message: Component name?
validate: required
- name: type
message: Component type?
type: select
choices:
- page
- component
- layout
files:
- template: "{{name}}.tsx.hbs"
destination: "src/components/{{pascalCase name}}/{{pascalCase name}}.tsx"
- template: "{{name}}.test.tsx.hbs"
destination: "src/components/{{pascalCase name}}/{{pascalCase name}}.test.tsx"
condition: "{{includeTests}}"
Template-Doc Linking
Templates can reference docs and vice versa:
In _template.yaml:
yaml
doc: patterns/react-component
In doc (markdown):
markdown
Use @template/react-component to generate.
AI workflow:
- •Get template config
- •Follow
doc:link to understand patterns - •Run template with appropriate variables
Handlebars Helpers
Templates use Handlebars with built-in helpers:
| Helper | Example | Output |
|---|---|---|
camelCase | {{camelCase "my name"}} | myName |
pascalCase | {{pascalCase "my name"}} | MyName |
kebabCase | {{kebabCase "MyName"}} | my-name |
snakeCase | {{snakeCase "MyName"}} | my_name |
upperCase | {{upperCase "name"}} | NAME |
lowerCase | {{lowerCase "NAME"}} | name |
CRITICAL: Template Syntax Pitfalls
JavaScript Template Literals + Handlebars
NEVER write $ followed by triple-brace - Handlebars interprets triple-brace as unescaped output:
code
// ❌ WRONG - Parse error!
this.logger.log(`Created: $` + `\{{\{camelCase entity}.id}`);
// ✅ CORRECT - Add space, use ~ to trim whitespace
this.logger.log(`Created: ${ \{{~camelCase entity~}}.id}`);
// Output: this.logger.log(`Created: ${product.id}`);
Rules when writing .hbs templates:
- •Never
$+ triple-brace - always add space:${ \{{ - •Use
~(tilde) to trim whitespace:\{{~helper~}} - •For literal braces, escape with backslash
When to Use Templates
| Scenario | Action |
|---|---|
| Creating new component | Run react-component template |
| Adding API endpoint | Run api-endpoint template |
| Setting up new feature | Run feature-module template |
| Consistent file structure | Use template instead of copy-paste |
Integrated Workflows
During Implementation (Use Template)
code
Task → Read Context → Find Template → Generate Code → Customize
- •Read task and understand requirements
- •List templates to find applicable one
- •Get template details and read linked doc
- •Run template (dry run first, then real)
- •Customize generated code as needed
- •Continue with remaining implementation
Benefits:
- •Reduces context (no need to generate boilerplate)
- •Ensures consistency with project patterns
- •Faster implementation
During Extract (Create Template)
code
Context → Identify Pattern → Create Doc → Create Template → Link Both
- •Identify repeatable code pattern
- •Create doc with
/knowns.extract - •Create template with
knowns template create <name> - •Link template to doc:
doc: patterns/<name> - •Link doc to template:
@template/<name>
When to create template:
- •Pattern will be used multiple times
- •Has consistent file structure
- •Can be parameterized
Checklist
- • Listed available templates
- • Got template details (prompts, files)
- • Read linked documentation (if any)
- • Understood required variables
- • Ran dry run first
- • Ran template with correct inputs
- • Verified generated files
Remember
- •Always dry run first before writing files
- •Check
doc:link in template for context - •Templates ensure consistent code structure
- •Create new templates for repeated patterns
- •NEVER write
$+ triple-brace - use${ \{{~helper~}}instead (add space, use tilde)