Example 3: Custom Method
In this example, we will expand second example, adding a custom method which is exposed through the templateData
argument on the renderTemplate
method
- Plugin Code
- Template
- Output
For example, if you have an arbitrary plugin method called `sayHello` it could look something like the following
import NPTemplating from 'NPTemplating'
export async function templatingHelloWorld(): Promise<void> {
try {
const templateData = {
methods: {
sayHello: (name: string = '') => {
return `Hello ${name}`
}
}
}
const result = await NPTemplating.renderTemplate('Template (Say Hello)', 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 (Say Hello)
*****
<%- sayHello('Mike') %>
Would produce the following output when the custom command is invoked
Hello Mike
Template Breakdown
The breakdown of this template is as follows
1: <%- sayHello('Mike') %>
line 1
: display some static textline 2
: invoke thesayHello
method which returnsHello Mike