If statement with Templater fails (Variable is not defined)

Things I have tried

What I’m trying to do

Hi everyone,

I just discovered Templater and I try to automate my note taking with a drop down box, and then make decisions based on the selected value.

After searching some templates, I found a few, utilizing the JS script block <%* %>, which I also adopted.

Anyone any idea?

I posted some code below. For testing purpose I always use “Online” in the drop down. Which is why I skipped the code for the other two.

Thanks in advance!

<%*
let FileName = await tp.system.prompt("Meeting name")  
titleName = FileName + " " + tp.date.now("YYYY-MM-DD")  
await tp.file.rename(titleName)  
await tp.file.move("/Meetings/" + titleName); 

let myFileType = await tp.system.suggester(["Onsite", "Remote", "Online"], ["Onsite", "Remote", "Online"])

if (myFileType = "Online") { 
	let Tags="Online";
}

tR += "---"
%>
title: <% FileName %>  
date: <% tp.file.creation_date("YYYY-MM-DD HH:mm:ss") %>  
tags: Meeting, <% Tags %>  
---

# <% qcFileName %>
<% tp.file.cursor() %>

Test output: <% Tags %>

The error message in the console is always “Tags is not defined.”

1 Like

Hey there!

I think the error lay in the variable scope of Tags. I don’t think that “Tags” was visible outside of the if statement.

I modified your code by declaring Tags before the if statement, then changing it’s value inside.

<%*
let FileName = await tp.system.prompt("Meeting name")  
titleName = FileName + " " + tp.date.now("YYYY-MM-DD")  
await tp.file.rename(titleName)  
await tp.file.move("/Meetings/" + titleName); 

let myFileType = await tp.system.suggester(["Onsite", "Remote", "Online"], ["Onsite", "Remote", "Online"])

let Tags = "";
if (myFileType = "Online") { 
	Tags="Online";
}

tR += "---"
%>
title: <% FileName %>  
date: <% tp.file.creation_date("YYYY-MM-DD HH:mm:ss") %>  
tags: Meeting, <% Tags %>  
---

# <% qcFileName %>
<% tp.file.cursor() %>

Test output: <% Tags %>

I should say this worked in my code once I removed the /Meetings/ directory, and also when I removed qcFileName, as this wasn’t recognised for me.

2 Likes

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