Link between template and folder

Hello !

I had an idea about templates (and, by extension, the templater plugin).
It could be interesting to link folders with templates: each time a new file is created, it will automatically take the assigned template.

On the workaround level, I was thinking of a field in the YAML that would be recognized by the plugin. For example, a folder field, which could take one or more folder names.

When creating a new note, if the file path is contained in one of the files in the template folder, then it will automatically take the template set.
If there is more than one, you just have to ask the user which one he wants to use.

Another idea could be the possibility to have, in the plugin options, the possibility to link the template files to a path. I think going this way might be faster/easier.
Eventually a shortcut could be added to the folder and keyboard for these notes with template.

2 Likes

Templater is flexible enough to do this. Templater can access the enclosing folder of a note with the tp.file.folder() function. If you write your template’s logic such that it is driven off of the output from tp.file.folder(), you can effectively link a folder to a template. Something like the following (untested) in a master template should work:

<%*
let folder = tp.file.folder();
switch (folder) {
  case 'Journal':
    tp.file.include('Templates/Journal');
    break;
  case 'Permanent Notes':
    tp.file.include('Templates/Permanent Note');
    break;
}
_%>

Admittedly, this is not so convenient as a configurable mapping between folder and template, but it is a decent workaround.

1 Like

Okay, so, one update of hotkey for template add this feature !

Modified your code a little to get it to work. Templater needs the links to be in wiki link format for the tp.file.include() and <%* doesn’t return anything so we need to use tR.

<%*
let folder = tp.file.folder();
switch (folder) {
  case 'People':
   tR += await tp.file.include("[[People Template]]");
    break;
  case 'Clients':
    tR += await tp.file.include("[[People Client Template]]");
    break;
}
_%>
2 Likes

Thanks for this Rishi. Am I right to assume that ‘People’ and ‘Clients’ here are folder names? And if they’re not in the root do they need the path assigned? 20 Work/People?

Responding to this sorry :slight_smile:

Yes. And no. In my example, Clients is actually a subfolder inside People, which is at the root level, and it works just fine.

1 Like