Templater tp.system.suggester with tp.file.create_new - New note is created regardless of selection

Hi All,

What I’m trying to do

  • I have folders: A1 and A2.
  • I have templates: “Template B1” and “Template B2”.

For notes in folder A1, I would like to use “Template B1”, which has a tp.system.suggester prompt with simple “Yes”/“No” options. If “Yes” is selected, a new note “B2” will be created (using tp.file.create_new) in folder A2 using “Template B2”. If “No” is selected, “No” will be added to the note in folder A1 and a note in folder A2 will not be created.

Things I have tried

<% tp.system.suggester(["Yes", "No"], [tp.file.create_new(tp.file.find_tfile("Template B2"), "B2", false, "A2"), "No"]) %>

Using templater’s logic, this should:

  1. Display a Yes/No suggester menu
  2. If “Yes” is selected > create a new note > using “Template B2” > name the new note “B2” > don’t open the note (false) > create the note in folder “A2”
  3. If “No” is selected > add “No” to the current note.

Issues

When using the above, it mostly works as expected. A suggester menu pops up, I can choose “Yes” or “No”, and if I choose “No”, “No” is entered into the current note. However, I find that note “B2” is created using “Template B2” in folder “A2” (all correct) regardless of the yes/no selection made (incorrect). In fact, I can see in my file navigation side panel while the suggester menu pop-up is open that note “B2” is created before a selection is even made.

I’ve tried using “await” before and adding parentheses around the “tp.file.create_new” function, but nothing seems to prevent “B2” from being created, even if I select “No” in the suggester menu.

Another curious item, of less importance, is that selecting “Yes” in the suggester menu results not only in note “B2” being created correctly, but also in “[object Object]” being added to the original note in folder A1. I’d like that not to happen.

Here you can see that “B2” has been created, even before an option as been selected:

And here is the “[object Object]” in the note in folder A1 when “Yes” is selected.

Screenshot 2024-11-27 at 22.18.11

1 Like

In you example you attempt to call the suggester with a call to the create new file. This call to create the new file is executed before the suggester causing your issue.

Lets revisit a simpler example: first( "1", second("2") ) . It’s easy to kind of assume that here we first call the first() function, and then later on call the second() function, but most programming language needs to know what they are going to call the first() function with, before actually calling, so therefore it examines the parameters, and sees a constant "1", and a function call second("2"). This second function call needs to be execute before it can call the first function. I hope that makes sense.


To solve your issue you need to shift around the logic, and add some await’s to make it behave like you want it to. Here is an untested variant, which I do believe should work better for you:

<%* 
const answer = await tp.system.suggester(["Yes", "No"], ["Yes", "No"])
if ( answer == "Yes") {
  await tp.file.create_new(tp.file.find_tfile("Template B2"), "B2", false, "A2")
} else {
 tR += "No"
}
_%>

Now we properly await the response of the suggester before acting on the result. If the answer is positive we await the creation of the new file, and if negative we add the text “No” to the output within the current file, using the tR special variable.

Thanks, worked like a charm. Just had to make one small tweak: add a semicolon before the “if” (I won’t pretend to know why, was getting a parsing error otherwise).

<%* const answer = await tp.system.suggester(["Yes", "No"], ["Yes", "No"]); if ( answer == "Yes") { await tp.file.create_new(tp.file.find_tfile("Template B2"), "B2", false, "A2") } else { tR += "No" } _%>

And thanks for the brief coding tutorial. Completely makes sense! Should note that this also got rid of the “[object Object]” result when “Yes” is selected.

The semicolon shouldn’t be needed of you have the code on multiple lines, but it was untested code so glad it works.