How to show a linked section only if it exists, else show something else with templater scripts?

Hi there,

I like to keep my Daily Note Template visually organized and decluttered. Therefore, I add my goals for the next day with a separate template in the evening, to keep it simple until then. My Daily Note Template refers to the goal section and ![]'s it in order for them to be displayed right when I open my new daily note the next day. What I do is linking a section with ![[<% %># Ziele für <% tp.file.title %>]]. This works fine for me, however, I don’t write down my goals every night, which means that the section cannot be found when creating the new daily note. This basically creates a huge heading saying “Unable to find section # Ziele für 2023-12-28 in 2023-12-27” of which I cannot change the appearence. Since, as I said, I like to keep my daily notes clean, I’m looking for a way to get rid of this.

What I’m trying to do

Is finding a javascript function that I can run with templater inside my daily note template which will either embedd the section if it exists in the previous daily note, or not display anything/a simple message like “no goals defined yesterday, do it now: - ” in the case of it not being able to refer to a specific section.

Things I have tried

I’m not at all good at handeling javascript and with the help of various online tools, I figured that this could somehow work:

function embedNotes() {
  const noteSection = "![[<% %># Ziele für <% tp.file.title %>]]";
  const sectionExists = !!document.querySelector(`a.internal-link[href$="${noteSection}"]`);

  if (sectionExists) {
    return noteSection;
  } else {
    return "no goals";


However, I’m getting error messages from Obsidian and I’m convinced that I can’t use the linking syntax in Javascript as it stands. Could someone please help me to figure this out?

Have good day,

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