How To Populate Title from Template

I’d like to populate Title when assigning a template to a new note. I’m reaching for the inverse of {{title}} in Templates plugin. Instead of inserting Title into the note, I want to populate Title from the text in the template.
What motivates this desire: I use Title prefixes to declare the type of note. For example, my notes related to companies all have a Title that begins with ‘Company -’

Things I have tried

I’ve read the documentation for Templates, Templater, Notes Composer, and Dataview plugins hoping that one of these would support my desire, but I don’t see how they will.

I’ve also searched this Forum, and discovered Using Dataview and to run icontains wherein Note title populates automatically from Templates that seems same or similar question; however, I got lost in the details (e.g. I don’t understand “icontains”)

2 Likes

I’m not sure I fully understand the question, but using Templater, you can apply the template after you create the note, then apply a template like this:

<% tp.file.path() %>

You can even have templater insert custom content (e.g. frontmatter perhaps) based on the file name. Like this (used from @tallguyjenks sample vault). This uses the first character of the file to determine the template to include, but could easily be changed to use the first word (for example) in the filename.

<%* if (tp.file.title.charAt(0) == "{") { %>
<%-tp.file.include("[[Templates/Inputs/Book]]")%>
<%* } else if (tp.file.title.charAt(0) == "@") { %>
<%-tp.file.include("[[Templates/Inputs/Person]]")%>
<%* } else if (tp.file.title.charAt(0) == "!") { %>
<%-tp.file.include("[[Templates/Inputs/Tweet]]")%>
<%* } else if (tp.file.title.charAt(0) == "%") { %>
<%-tp.file.include("[[Templates/Inputs/Podcast]]")%>
<%* } else if (tp.file.title.charAt(0) == "+") { %>
<%-tp.file.include("[[Templates/Inputs/Youtube]]")%>
<%* } else if (tp.file.title.charAt(0) == "(") { %>
<%-tp.file.include("[[Templates/Inputs/Article]]")%>
<%* } else if (tp.file.title.charAt(0) == "&") { %>
<%-tp.file.include("[[Templates/Inputs/Paper]]")%>
<%* } else if (tp.file.title.charAt(0) == "=") { %>
<%-tp.file.include("[[Templates/Inputs/Thought]]")%>
<%* } else { %>
<%-tp.file.include("[[Templates/Inputs/New]]")%>
<%* } _%>

The example Company-Note template for Templater below is based on one I found when I first discovered Obsidian (I cannot re-find the original source to give appropriate credit).

---
<%*
   let title = tp.file.title;
   if (title.startsWith('Untitled')) {
      title = await tp.system.prompt('Company - ');
      await tp.file.rename(`${title}`);
   }
-%>
title: <%* tR += `${title}` %>
created: <% tp.date.now('YYYY-MM-DD') %>
kind: company
---
# <%* tr += `${title}` %>
<% tp.file.cursor() %>

The important thing is that it changes the note’s title using the tp.file.rename function if it has not previously been entitled.

Thanks. This looks like what I want to achieve and the user interface behaves as I would expect (prompting me for the template to apply and then asking me for the company name).
Alas, I get a “Templater Error: Template parsing error, aborting. Check console for more information”
From the Console I see “tr is not defined”
As a non-developer, I’m in over my head, but I’ll dig into this some more to learn as soon as I can free some time.
Thanks again.

I think there is a capitalization typo in the line that starts #: tr is supposed to be tR.

No idea what that stands for or why whoever wrote the templating framework that templater is based on decided that was a good name.

@scholarInTraining is right about the typo. It should be a capital ‘R’ instead of lower case.

# <%* tR += `${title}` %>

Note, though, that this is just an example of what you can include in the template.

1 Like

Good catch. Indeed now works once editing tr to tR. Thanks.

1 Like

Understood regarding “just an example”. Now that I have the example working I can evolve further to my desired. Thanks again.

1 Like

Thank you for this! It was a big help for finalizing my YouTube video template (for logging videos I’ve watched).

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