Template Tags
Templating
uses a tagging system to indicate where dynamic content will be generated within template notes.
Tags
<%
Script tag to be used when executing any JavaScript command without producing any output<%-
Outputs the value into the template<%_
'Whitespace Slurping' Template tag, strips all whitespace before it (example)<%#
Comment tag, no execution, no output<%%
Outputs a literal<%
-%>
'Whitespace Slurping' Template tag, strips all whitespace after it (example)%>
Plain ending tag
Output Tags
When you wish to output anything to the rendered template, you use the output <%-
Examples
The following are various examples of Templating
tags in action
JavaScript / Flow Control Tag
The basic flow control tag <%
is used when you want to perform a standard JavaScript action such as setting a variable, doing a calculation, looping or conditionals. In this example, a getData
method would be called, but the actual output would be displayed in another section of template. If you have many lines of JavaScript, you may want to consider the templatejs code block.
<% const data = getData() // the variable is set in this tag %>
...
<%- data // the output is output later in this tag %>
Note that flow control tags, like all other tags are on a line by themselves. This means there is a return at the end of the line. When the template is rendered, the tag will be processed and deleted, but the return at the end of the line will show up in your rendered template as a blank line. If you don't want the returns, use the minus (slurping tag) at the end of each flow control line, e.g.
<% const myVar = note.title // there will be no blank line when this is processed because of the dash: -%>
Standard output tag (variable)
Display first
name variable contained in Templating
Plugin Settings
<%- user.first %>
Standard output tag (module method)
Displays current date from Date Module
<%- date.now() %>
Unescaped Output Tag
Displays result from templates.services.developerQuote
defined in Templating
Plugin Settings
Templating
doesn't escape characters by default. When doing web requests, it may be useful to escape dangerous characters. You can escape a command's response characters using the <%-
tag.
<%- web.service('developerQuote') %>
Strip Whitespace
When you have have process tags (this which do no produce output), it is recommended that you use the Whitespace Slurping
tags.
<%_ const testName = 'Mike' -%>
name: <%- testName %>
will produce the following output
name: Mike
templatejs Code Block
To make coding in templates easier, you can add a code block with the language "templatejs", and use all the variables that are generated in this code block inside your template. You can use JavaScript in this code block and have full access to the JavaScript Plugin API, and templating modules. An embedded code block helps if you have a lot of JavaScript code like in this example.
For example:
---
title: Overdue Tasks
type: meeting-note, empty-note
---
// Get all overdue tasks
const overdues = await DataStore.listOverdueTasks()
// Get the overdue task count
const overdueCount = overdues.length
// Get the latest overdue task
const latestTask = overdueCount > 0 ? overdues[0] : undefined
// Create a variable that holds the content if there is a latest task
let taskContent = ""
if(latestTask) {
taskContent = latestTask.rawContent
}
Overdue Tasks: <%- overdueCount %> <%- taskContent %><br>
This will print the amount of overdue tasks and the latest overdue task below.
Learn here about how you can use JavaScript in general in your templates, and if you don't have that many lines of code (means you don't need a code block).