Templater Help: If statement

Hi folks, i have this global templater:

<%* let parent_tfile = tp.config.active_file;
let parent_cache = app.metadataCache.getFileCache(parent_tfile);
let tag= parent_cache.frontmatter[“tags”];
let tit= parent_tfile.basename;
%>

aliases:
tags: [<% await tag %>]

:small_red_triangle:: [[<% await tit %>]]

It takes in consideration the active note that im using to work, but when i create a new note from a empty note it breaks.

How i could add a if statement there to make it works from a empty note ?
Obs: I use the CTRL-N to "create a new note from templater "

Thanks in advance

think something like that , but its not working

<%*
if (tp.config.active_file){
let parent_tfile = tp.config.active_file;
let parent_cache = app.metadataCache.getFileCache(parent_tfile);
let tag= parent_cache.frontmatter[“tags”];
let tit= parent_tfile.basename;
}
else {
let tag = “”;
let tit = “”;
}
%>

How about the following:

<%*
let tag = '';
let tit = '';
if (!(tp.file.title.startsWith('Untitled')))
{
   let parent_tfile = tp.config.active_file;
   let parent_cache = app.metadataCache.getFileCache(parent_tfile);
   tag= parent_cache.frontmatter['tags'];
   tit= parent_tfile.basename;
}
-%>

almost bro,
it didnt break when i launch in a empty file,
but when i launch in a active file with content note,
the stuffs inside the if didnt work , the new file didnt get the “tag” and “tit” variable contents from the file before

i think the problem is to make templater know that im triggering it from a blank obsidian note (“no file is open”), like how to make templater know this state ?

Maybe I misunderstood what you’re trying to do.

Are you saying that you want to create a new note that starts with the tags of the note you’re currently working with and a link back to that note?

If so, then the above won’t work, because by the time the template is being interpreted, the active_file is pointing to the new, empty note.

basically i trigger template in both ways:

1- when i have a note in the background (most of times), where the templater picks the tags and title of the background and populate the new one

2- When there is no note in the background (like when you open a new tab) and wants to create a new note from the template

the original code that i posted, worked with the first situation, but when i tried in the second situation it gives me a error , since there is no note in the background to it pull the tags and titles (i receive something called parse error )

So i would like to put a “IF” to it understand the both situations , but so far i didnt be able to make templater recognize the second situation

How about this then:

let tag = '';
let tit = '';
try
{
   let parent_tfile = tp.config.active_file;
   let parent_cache = app.metadataCache.getFileCache(parent_tfile);
   tag= parent_cache.frontmatter['tags'];
   tit= parent_tfile.basename;
}
catch {}

no but surprisingly chat gpt yes,

<%*
let tfile = tp.config.active_file || “”;
let cache = app.metadataCache.getFileCache(tfile) || { frontmatter: { tags: [“”] }};
let tag= cache.frontmatter[“tags”] || “”;
let tit= tfile.basename || “”;
%>

thanks for your support bro

One last thing , the code above is crashing the frontmatter below, like this

<%*
let tfile = tp.config.active_file || “”;
let cache = app.metadataCache.getFileCache(tfile) || { frontmatter: { tags: [“”] }};
let tag= cache.frontmatter[“tags”] || “”;
let tit= tfile.basename || “”;
%>


aliases:
tags: [<% await tag %>]

Frontmatter is not rendering well, you know how to fix ?

I’m not sure what you mean by crashing or not rendering well.

There are a few things I see that could be improved.

  1. Apply the same logic that you use in the let cache = line to the let tfile = line. Namely, supply a dummy object rather than an empty string. When you later use this variable you won’t need a || "".
  2. The || "" is unnecessary in the let tag= line. The line before assures that you’ll have a valid value for cache.
  3. Coming from metadataCache, the tags is returning an array. Given the way you later use it, you should convert this to a string using join.
  4. Put a - in front of %>. This will prevent that code block from leaving an empty line behind after the template is processed. That’s especially important here, since the first line of the file must be three dashes for the frontmatter to be recognized as such.
  5. Make sure the first line after -%> contains only three dashes (---) and nothing more.
  6. Make sure the line immediately after your last frontmatter value contains only three dashes (---) and nothing more.
  7. There should not be an await before the tag. That’s only used when accessing asynchronous functions.
  8. Earlier you included a link to the source note. However, I read in a different forum post that links in frontmatter don’t work (It would get interpreted as an array containing an array of one item, I believe.) As such you’ll probably want to put that item outside the frontmatter.

Taken the items above together, the template should look like:

<%*
let tfile = tp.config.active_file || { basename: "" };
let cache = app.metadataCache.getFileCache(tfile) || { frontmatter: { tags: [""] }};
let tag = cache.frontmatter["tags"].join(', ');
let tit = tfile.basename;
-%>
---
aliases: []
tags: [<% tag %>]
---
(emoji): [[<% tit %>]]

I don’t know if any of this will solve your issues, though.

[Aside: when showing multiple lines of raw code in the forum or in your Markdown notes, enclose the content with three back ticks before and three after. ```]

2 Likes

thanks bro, helped a lot

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