Templater: Trigger Templater on new file creation

I have installed the Templater plugin and thoroughly read all the official Templater docs.

There is a config setting “Trigger Templater on new file creation” with the explanation “Templater will listen for the new file creation event, and replace every command it finds in the new file’s content.”

I don’t get it. If I create a new file, then the file is empty. So, there is actually no content in the file as it is new. What “command … in the new file’s content” is the explanation about? How to use this feature?

1 Like

The whole point of using templates is that they are NOT empty files. They contain things you want to have in the files you create, like metadata, headings, and so forth. And if you use placeholders in the template, they can be replaced by pieces of information, such as the date and time (and many other things).

Have a look at this video, which may help: https://youtu.be/5j9fAvJCaig. Nicole provides extremely clear explanations.

Of course, templates are not empty files. But the explanation/feature I’m puzzling about refers to the “new files” which are NOT templates. (Unless you’re practicing meta-templating.)

If I create a new file, I have the command “Create new note” in mind which creates a new and empty note/file. Unless I created the note/file in a specific folder for which I enabled a folder template (Templater config section “Folder Templates”). Then the template “responsible” for the folder takes over and prefills the new file. That’s what Nicole explains in the video referenced by you. But that’s about the config setting “Folder Templates”, not about the “Trigger Templater on new file creation”.

So, unfortunately, I cannot find an answer to my question in Nicole’s video.

Or is the sole purpose of toggling “Trigger Templater on new file creation” just to let appear the Templater setting “Folder Templates”?

I haven’t found a way to update/enhance my original post. So, I’m doing it in this separate post.

My original post could be also be paraphrased as:

I am aware of the Templater setting/feature “Folder Templates” but that one is not the subject of my question. The subject of my question is the setting/feature “Trigger Templater on new file creation” with the setting “Folder Templates” disabled. What is the difference in Obsidian/Templater behavior if I solely toggled “Trigger Templater on new file creation”?

Is there a way to manually create a new file/note that is not empty? If so, how to tell Templater which template to execute on the new note?

1 Like

You do need to combine the two, trigger on new file, and a template linked to the root folder, /. If not templater don’t know which template to apply.

One could argue why the duality, but that’s related to how many ways files can be created within Obsidian, and it’s just the way it is.

In addition, but this is not a must, you can create additional templates for other folders as well.

1 Like

So, to be clear, using tp.file.create_new doesn’t work, right?

Here is the syntax reference from the documentation:

tp.file.create_new(template: TFile ⎮ string, filename?: string, open_new: boolean = false, folder?: TFolder)

It has this description:

“Creates a new file using a specified template or with a specified content.”

It has this example:

File creation: [[<% (await tp.file.create_new("MyFileContent", "MyFilename")).basename %>]]


I have memory of being able to do this in the past, but am not sure I am remembering correctly, or whether this was the command I used.

Thanks for any information.

So, I’m assuming your original post was referring to this item in preferences:

I guess this is not perfectly clear. What I assume the writer to mean is that when a new file is created using a template, the placeholders that are in that template (the “commands”, in other words) will be replaced with the relevant information. I know this works, because my daily notes are created using a template that has Templater placeholders in it (for tomorrow and yesterday) – but the creation of the note is not triggered by Templater, it is triggered by using “Create New Daily Note with Template” (or whatever the command is – I actually use the Periodic Notes plugin). In other words, you can have various plugins create notes, and insert information using Templater syntax, that information being called on note creation.

I hope I’ve got that correct. Others will correct (I hope) if I have not.

Bear in mind that you can also use the command palette to create a new note from a template, which will bring up a chooser so you can select the template:

1 Like

Why shouldn’t it work? It’s what’s worked for me.

What doesn’t work in your case?

1 Like

Is there plugin functionality that can be triggered upon new note creation?

An example for me is when I click on a link that has not yet been instantiated as a note, I’d like to do something at that point using templater or quickadd or …

1 Like

What is confusing me is that I remember being able to format the template script so as to add different content on new lines.

For example, I know that something like this works:

File creation: [[<% (await tp.file.create_new(
"MyFileContent"+tp.date.now("YYYY-MM-DD HHmmss"), 
tp.date.now("YYYY-MM-DD HHmmss"))).basename %>]]

But something like this, where you might want the date on a new line in the created file, doesn’t work:

File creation: [[<% (await tp.file.create_new(
"MyFileContent"
tp.date.now("YYYY-MM-DD HHmmss"), 
tp.date.now("YYYY-MM-DD HHmmss"))).basename %>]]

And, if you add the required + then it will simply add the date immediately after MyFileContent:

File creation: [[<% (await tp.file.create_new(
"MyFileContent"+
tp.date.now("YYYY-MM-DD HHmmss"), 
tp.date.now("YYYY-MM-DD HHmmss"))).basename %>]]

I guess what I am saying is that it does work, but it doesn’t work the way I thought I remembered it working. Perhaps there is a simple way to add a new line in Templater scripts.

My recollection is being able to create a separate Templater script note and either refer to that in the script or choose it via a suggester. Regardless, if there is a way to add the new line, then that will solve what is getting me stuck.

Thanks @holroy for all your help here and throughout the forum! Amazing stuff!

1 Like

Try something like this:

File creation: [[<% (await tp.file.create_new(
"MyFileContent\n" +
tp.date.now("YYYY-MM-DD HHmmss"), 
tp.date.now("YYYY-MM-DD HHmmss"))).basename %>]]

Or my favorite is this variant:

File creation: [[<% (await tp.file.create_new(
`## Nice header

Some text stuff...


Today is: ${ tp.date.now("YYYY-MM-DD HHmmss") }, but what's in a date? 
`,  tp.date.now("YYYY-MM-DD HHmmss"))).basename %>]]

That is use single backticks around the entire text, and use ${ ... } around variables.

4 Likes

Thanks! New possibilities have been opened! I greatly appreciate this!!!

1 Like

Thanks for all the answers and ideas. Based on all your input, I have found the solution to my questions. For people who could be puzzled with the same questions, I sum up the answers:

Is there a way to manually create a new file/note that is not empty? If so, how to tell Templater which template to execute on the new note?

My initial misconception was that a new file/note is always empty. This is a false assumption: in Obsidian context, “new” solely means that the file has been “seen” by Obsidian for the first time (as a note), so that an Obsidian “new file creation event” is generated. The file itself - from the file system’s view - could have been existed before it has been detected by Obsidian, e.g. if you moved an existing file with existing content into Obsidian vault’s folder.

What is the difference in Obsidian/Templater behavior if I solely toggled “Trigger Templater on new file creation”?

Since the “new” file could have some content, this content could contain some Templater commands. Activating the option “Trigger Templater on new file creation” causes Templater to listen to Obsidian vault’s event “create” and to evaluate the content of the “new” file as a Templater template.

This behavior can be verified by a simple experiment:

  1. Create a random Templater template file outside of your Obsidian vault.
  2. Enable the Templater option “Trigger Templater on new file creation”.
  3. Copy the template file into your Obsidian vault.
  4. Verify that the file has been processed by Templater as if you had had triggered the command “Templater: Replace templates in the active file”.
  5. Delete the file/note.
  6. Disable the Templater option “Trigger Templater on new file creation”.
  7. Copy or move the template file (from step 1) again into your Obsidian vault.
  8. Verify that the file has been left untouched by Templater.

This results in the known security warning, that I only now understand in its full scope:

If the option “Trigger Templater on new file creation” is activated, never blindly copy or move any MD files to your Obsidian vault! Also: never blindly change the extension of not MD files to MD files inside your Obsidian vault - that would trigger a “create” event as well and cause Templater to process the file’s content; if the file contains malicious Templater commands, you could get hacked.

1 Like

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