Templator commands not working but are being recignised?

Hi,

So I am having issues with getting templator commands running. They are being recognised I assume by the change in the colour when I write it out, but they do not execute.

Is there anything I am doing wrong?

I should mention that under the date section, I have just used the standard obsidian {{date}} command.

Thank you!

You need to run your template with Templater for your <% ... %> to work and output the desired results :blush: …

I can only guess here, that you’ve applied your template using the Core Template plugin of Obsidian as the {{date}} worked :innocent: .

You can’t mix and match the syntax of the Template Core plugin with Templater’s one, as they are incompatible :blush::

  • Things like {{date}} will only work with the Template Core plugin (as only that plugin knows what to do with it)
  • Things looking like <% ... %> will only work with Templater (for the same reason as previously: Only Templater can make them work)

So, generally speaking, you either build a Templater’s template and apply it solely with Templater… or, you build a Core Template plugin template and use it with that plugin.
It’s one or the other :blush: .

You could though try to use the Templater command: Replace templates in the active file on the note your shared as screenshot … This could give the expected results for that specific note…

You should probably replace your {{date}} placeholder by something Templater can use (tp.date.now()) and only use that template with Templater :blush:

1 Like

Hi Pch,

Thank you for your response! I apologise It’s taken me so long to respond, I have been very busy recently.

I have tried to implement your suggestions but when I did I kept getting errors.

image

and when I deleted some of the templater code I got a different error. I have tried to understand what it means but not only can I not find the console, I also don’t understand what is wrong with the code.

image

I have copied this code from a YouTube video of someone setting up and implementing this setup. I have also run this code separately and used the “Templater: Replace templates in the active file” command and they seem to do as expected, but when pulling from the daily notes template templater shortcut it just throws these errors.

Would you happen to know why this is the case?

Thanks again for your response, I hope you’re having a great year so far!

This means that there’s something wrong with tp.file.title and the format you said it would follow in tp.date.now() …

And I guess that this is because you only “name” the new Untitled note with the current date after the frontmatter block in which you already use tp.file.title as the reference date (or something similar)

I can only assume here that your current workflow is to create a “Today’s Daily Note” from an initially “Untitled” note (otherwise, you wouldn’t need a tp.file.rename() anywhere :sweat_smile: )

I’ve tested this Templater template (based on your last screenshot), applied to a note titled “Untitled…” :

<%*
// Declare "today"
// Used to rename the note and as a reference date within tp.date.now() (instead of
// tp.file.title)
const today = tp.date.now("YYYY-MM-DD");

//Rename "Untitled" note with "today"
// (only if the note's title is "Untitled...")
if (tp.file.title.startsWith('Untitled')) {  
await tp.file.rename(today);
}
-%>
<% "---" %>
tags: DailyNotes
related_to: "[[<% tp.date.now("GGGG-[W]WW", 0, today, "YYYY-MM-DD") %>]]"
aliases:
- <% tp.date.now("dddd Do MMMM, YYYY", 0, today, "YYYY-MM-DD") %>
created: <% today %>
mood: 
<% "---" %>

> [!review] Review
> Previous:: [[<% tp.date.now("YYYY-MM-DD", -1, today, "YYYY-MM-DD") %>]] 
> Next:: [[<% tp.date.now("YYYY-MM-DD", 1, today, "YYYY-MM-DD") %>]] 
> Parent:: [[<% tp.date.now("YYYY-MM", 0, today, "YYYY-MM-DD") %>]], [[<% tp.date.now("GGGG-[W]WW", 0, today, "YYYY-MM-DD") %>]]  

<% tp.file.cursor() %>

Which ouput (while renaming Untitled into 2024-01-15) this:

---
tags: DailyNotes
related_to: "[[2024-W03]]"
aliases:
- Monday 15th January, 2024
created: 2024-01-15
mood: 
---

> [!review] Review
> Previous:: [[2024-01-14]] 
> Next:: [[2024-01-16]] 
> Parent:: [[2024-01]], [[2024-W03]]  

As you can see, I don’t rely on tp.file.file as a reference date but on the constant today declared at the top of the template (which I use and re-use where needed).

When it comes to the frontmatter block/YAML/Properties the few things to note is that:

  • if a value of a key is a wikilink, the wikilink needs to be quoted

    related_to: "[[2024-W03]]"
    
    • instead of
       related_to: [[2024-W03]]
      
  • the aliases key is of type list (which can’t be changed) … So its value(s) needs to be written as such, which can be done 2 ways:

    aliases:
      - alias 1
      - alias 2
      - alias 3
    

    … or as an inline array but Obsidian will reformat them as a bulleted list if you interact with Properties

The last thing I’ve spotted in your screenshot is the format used for the “weeks” : "YYYY-[W]" … This would only output 2024-W.
Square brackets are used, in date & date/time format, to escape things that shouldn’t be parsed by MomentJS when the template is applied :blush:. So I changed it and used GGGG-[W]WW instead :innocent: .

Now, it would probably easier if you used the Daily Notes core plugin or Calendar and/or Periodic Notes to create your daily notes :innocent: … As the daily note would be created where you want it, with the template you want and already titled with “today’s date” (So there would be no need to rename an Untitled... note) :blush:

1 Like

Hi Pch,

To say you have been a great help is a huge understatement.

Thank you so much for your help and for taking the time to respond to my questions and explaining the reasoning behind your solutions.

You are awesome!

Just a quick question. Where/how did you find out to do this? How can I go and learn the ins and outs of obsidian plugins the way you do? I have read the documentation for templater but I really didn’t pull the same conclusions you did :laughing:

Have a great day! :grin:

1 Like

My pleasure :raised_hands: :grin: !

Well :sweat_smile: … In summary, with practice and loads of tests and experiments and lots of reading too :laughing: …

When it comes to the “renaming issue”, knowing it wouldn’t work because it was “misplaced”/'mis-used" is much more due to my raw and basic knowledge of Javascript in which you generally need to declare “anything” (constants/variables) before being able to use them later on, as the code runs from top to bottom (which is a principle Templater’s templates also follow).

Other than that (and looking at the available documentation), to be honest, I just like to look at what others do with Templater, get some inspiration from snippets some shared freely here and there (on the forum, on Obsidian Discord server and its Templater channel, on the web), try to understand what they do and how they do it to then try to adapt these to my needs (or, recently, more often than not, to the needs of others :smile: ), learning things along the way :blush: .

This all helped me to slowly improve my “Templater(/JS)” skills over time and I’m not done learning :innocent: !

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