Templater: tp.file.title does not show the updated title after rename the file

I want to start a not, using a template that pre-write part of the title of the note

Example:
I want the note start saying "1-1 "+atendee+“YYYY-MM-DD”

I did this:
<%*
let title = tp.file.title;
if (title.startsWith(‘Untitled’)) {
title = await tp.system.prompt('Atendee: ');
await tp.file.rename("1-1 "+${title} +tp.file.creation_date(“YYYY-MMM-DD”));
}
-%>

<#[[% tp.file.title %>]]

It works perfect and add the attendee to the file name, but the line tp.file.title, continue saying “Unknown”

If I remove the code… for adding “1-1”… tp.file.title, works perfectly well, if I change the Unknown in the filename, the tp.file.title shows the change in the name.

What I am doing wrong?.. thank you.

Why don’t you use title instead of the tp.file.title, which I presume is just set when the template initiated?

If I use title It only shows me the attendee, and not the complete title: 1-1 atendee 2023-Sep-8

Missed out on the extension of your title, but then you could store the full filename in a variable, and then use that later on in the file.

My main point that tp.file.title is set at the start of the template, and doesn’t change during the template execution is still valid (I think).

It could be argued that it possibly should change after a rename operation, but there are also valid reasons why it shouldn’t. So keeping track of it yourself is the better solution in this case.

Just to illustrate :innocent: :

<%*  
let title = tp.file.title;
let created = tp.file.creation_date("YYYY-MM-DD");
let attendee = await tp.system.prompt('Attendee: ');
let newTitle = `1-1 ${attendee} ${created}`; // Declaring newTitle  
 
if (title.startsWith('Untitled')) {  
await tp.file.rename(newTitle); // Re-using newTitle  
}  
-%>
# [[<% newTitle %>]]

If you want to counter for the case that the file isn’t renamed you could do it this way:

<%*  
let title = tp.file.title;
let created = tp.file.creation_date("YYYY-MM-DD");
let attendee = await tp.system.prompt('Attendee: ');

if (title.startsWith('Untitled')) {  
  title = `1-1 ${attendee} ${created}`; // Set and store the new title
  await tp.file.rename(title); 
}  
-%>
# [[<% title %>]]
1 Like

Thank you all! it worked perfectly fine.

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