<%*
let title = tp.file.title
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("Title");
await tp.file.rename(title);
}
-%>
---
tags: <% tags %>
aliases:
title: <%* tR += title %>
created: <% tp.date.now() %>
---
<%tp.file.cursor(1)%>
Then, if I run “Create new note from template” I don’t expect the default template being triggered, but in practice it is being triggered, and because the default template has a prompt for a title, I get the prompt.
Now let’s say I have a second template with this content:
<%* let title = await tp.user.ensure_title(tp) -%>
# <%* tR += title %>
The user script looks like this:
module.exports = async function (tp) {
let title = tp.file.title
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("Title");
await tp.file.rename(title);
}
return title;
}
If I now call ‘create file from template’ using the second template as argument, I get a prompt twice, one coming from the default template, and the final one coming from my second template. The result is a correctly generated page, but I’m wondering why is the default template being triggered. If I input two different values, only the second value gets used, what makes me thing that the order of execution is:
Default template
Selected template
Default template means that I have it setup to run when a note in the folder / is created.
Have you tried to use a lockTemplateCreation (or whatever you want to name it) field attached to the tp object in the script?
You could then use it to check for it in every template as a first action and skip anything if true or equals a certain value: conditional templating is your friend.
There is an alternative if you are willing to install another plugin.
With CustomJS you can define a class which can be loaded at startup. The plugin creates an instance that can be used anywhere where JavaScript can be used, e.g., within a Templater script.
According to Templater’s documentation: " Scripts should follow the CommonJS module specification, and export a single function.". Hence, you can return a class object from that function and have the same functionality.
I intensively use Templater for “templating”. What I miss is the flexibility to save my JavaScript scripts wherever I want to – you have to save them in one folder. The exception of the rule are startup scripts.
CustomJS is more supportive in that respect. It is a matter of preference.
My experience, especially when one template moves the current note: duplicate files in different folders with different content, i.e., one with no content and the other with the template applied.
There might be other results, but that is guesswork: mixed-up content etc.