How to avoid the error from a templater plugin YAML script when I press the escape key to break it?

I have stolen this snippet fragment which lives in every YAML front matter of my templates from templater. It gives the new file a name via a dialog window.


---<%*
let title = tp.file.title
if (title.startsWith("Untitled") || title.startsWith("Unbenannt")) {
    title = await tp.system.prompt("[TEMPLATE Neue Notiz] Bitte den gewünschten Dateinamen eingeben:");
    await tp.file.rename(title);
    tp.file.title = title;
} %>
---

But when I press <Strg-n> to make a new file and then (when the dialog window shows up) press the ‘escape’ key, this error occures:

Templater Error:

Template parsing error, aborting. 
Check console for more information.

How can this error-message be avoided? Somehow the abort has to be intercepted.

You need to add a check for NULL after the prompt, something like this…

let title = tp.file.title
if (title.startsWith("Untitled") || title.startsWith("Unbenannt")) {
    title = await tp.system.prompt("[TEMPLATE Neue Notiz] Bitte den gewünschten Dateinamen eingeben:");
    if (title != null) {
        await tp.file.rename(title);
        tp.file.title = title;
    }
} %>

Thank you very much, this works as intended.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.