Define a variable based on another variable in templater

What I’m trying to do

I would like a template that creates a dropdown box via tp.system.suggester, and the selected result becomes basis for defining new variables.

For example, in the code I tried, the dropdown box has two options for constant “notetype”. If user picks option “experimental”, this should trigger defining another constant, called “title”.
I can then use that new constant to set title and folder.

Things I have tried

<%*
const notetype = await tp.system.suggester([“experimental”, “article”], [“experimental”, “article”]);

if (notetype === “experimental”) {
let title = “expfolder”
}
await tp.file.rename(${titlefront + titleback})
%>

This doesn’t work. I get the dropdown box, but then “templater parsing error”.

I have a script that does this in a step-wise way: dropdown box first for file name, then for folder name, then for specific tag. All three are depending on the type of note, so if I could set that type once, and then via script define everything else, it would save couple seconds each time I create a new note.

In the spirit of outsmarting myself, I spent a whole afternoon trying to figure out a script that would save me couple seconds every day.

You have a couple of obvious errors that need to be addressed before you can even resolve why this isn’t working. Assuming this is the entirety of the script, you’re missing a lot of code.

First, in the rename function you list titlefront and titleback, but those are never defined anywhere. So you’re saying, “rename to undefined+undefined”, which is probably throwing the error.

But even before that, in your if statement, you have let title = "expfolder". But if you declare a variable inside of an if statement, they only exist within the brackets of that statement. So essentially, you are creating a variable called title, but doing nothing with it within the if so it’s just forgotten. Which also means that if you try to use title later, it never got created at all if the user selected “article” which would also throw an error.

If you want to use it outside of the if, you need to declare it outside of the if:

const notetype = await tp.system.suggester([“experimental”, “article”], [“experimental”, “article”]);
let title = "";

if (notetype === “experimental”) {
  title = “expfolder”;
}

Once you get that sorted out, you’ll be in a better place to get your template working.