Example 2: Custom Variables
In this example, we will expand on first example, adding a custom variable which is exposed through the templateData
argument on the renderTemplate
method
- Plugin Code
- Template
- Output
For example, if you have an arbitrary plugin method which would output a variable, it would be constructed as follows
import NPTemplating from 'NPTemplating'
export async function templatingHelloWorld(): Promise<void> {
try {
const templateData = {
data: {
name: 'Mike'
}
}
const result = await NPTemplating.renderTemplate('Template (Variable)', templateData)
Editor.insertTextAtCursor(result)
} catch (error) {
console.log('templatingHelloWorld', error)
}
}
Then, you will have a templated name Template (Hello World)
located in your NotPlan template folder with the following
# Test (Variable)
*****
<%- name %>
Would produce the following output when the custom command is invoked
Mike
Template Breakdown
The breakdown of this template is as follows
1: <%- name %>
line 1
: display the value inname
variable