Templater: create new note from template issues

I’m currently using a custom template with tp.user.main(tp). When I call the function mentioned in the title, it creates an Untitled note in the vault folder. I’m not sure if its an issue with my JavaScript code or if there’s something going on with Templater. The other issue I’m having is that when I create a new note using that command, it opens up the new Untitled note instead of the one I’ve created. I’ve looked through the code and haven’t seen anything odd - it was working the other day with no issue. Below is the output of my script.

fs: initialized  {access: ƒ, copyFile: ƒ, cp: ƒ, open: ƒ, opendir: ƒ, …}
VM454:5 tp: initiliazed  TemplaterPlugin {_loaded: true, _events: Array(13), _children: Array(0), _lastDataModifiedTime: 0, onConfigFileChange: ƒ, …}
VM454:310 main: starting
VM454:311 main: getting title
VM454:315 main: title  TEST
VM454:317 main: calling getTag
VM454:14 getTag: starting
VM454:19 getTag: tags file path:
 /home/df/Obsidian/math/Information/Tags.md
VM454:20 getTag: reading tags file
VM454:23 getTag: tags  (12) ['#axiom', '#concept', '#corollary', '#definition', '#formula', '#institution', '#lemma', '#person', '#proof', '#reference', '#subject', '#theorem']
VM454:26 getTag: chosenTag  #axiom
VM454:27 getTag: slicing leading #
VM454:29 getTag: returning chosenTag
VM454:319 main: returned tag  axiom
VM454:321 main: letting frontmatter
VM454:323 main: calling generateFrontmatter
VM454:145 generateFrontmatter: starting
VM454:147 generateFrontmatter: tp.file.creation_date as id
VM454:149 generateFrontmatter: id  20231114201134
VM454:151 generateFrontmatter: tag  axiom
VM454:152 generateFrontmatter: checking tag case
VM454:154 generateFrontmatter: calling getSubject
VM454:44 getSubject: starting
VM454:49 getSubject: absolute path:
 /home/df/Obsidian/math/Zettelkasten/Subjects/
VM454:51 getSubject: reading fs
VM454:54 getSubject: subjects  (13) ['Abstract Algebra', 'Analytic Geometry', 'Calculus', 'Functions', 'History of Mathematics', 'Linear Algebra', 'Logic', 'Number Theory', 'Ordinary Differential Equations', 'Partial Differential Equations', 'Probability', 'Real Analysis', 'Set Theory']
VM454:57 getSubject: chosenSubject  Abstract Algebra
VM454:59 getSubject: returning chosenSubject
VM454:156 generateFrontmatter: returned subject
 Abstract Algebra
VM454:158 generateFrontmatter: getting alias
VM454:160 generateFronmatter: returned alias
: TEST
VM454:163 generateFrontmatter: generating axiom frontMatter
VM454:174 generateFrontmatter: calling getReference
VM454:74 getReference: starting
VM454:78 getReference: absolute path:
 /home/df/Obsidian/math/Zettelkasten/References/
VM454:80 getReference: reading fs
VM454:83 getReference: references  (8) ['Calculus with Analytic Geometry', 'ChatGPT', 'Cracking the GRE Mathematics Subject Test', 'Ordinary Differential Equations', 'Real Analysis Flashcards', 'The Organic Chemistry Tutor', 'The Pleasures of Probability', 'Wikipedia']
VM454:86 getReference: chosenReference  Calculus with Analytic Geometry
VM454:88 getReference: returning chosenReference
VM454:176 generateFrontmatter: returned reference
 Calculus with Analytic Geometry
VM454:197 generateFrontmatter: returning frontMatter
VM454:198 generateFrontmatter:
 ---
tags:
  - axiom
id: 20231114201134
subject: "[[Abstract Algebra]]"
reference: "[[Calculus with Analytic Geometry]]"
alias: "TEST"
---

VM454:326 main: letting body
VM454:328 main: calling generateBody
VM454:207 generateBody: starting
VM454:209 generateBody: checking tag case:  axiom
VM454:210 generateBody: updating body to axiom
VM454:253 generateBody: returning body
VM454:254 generateBody:
 ### Fact:

VM454:331 main: pulling up file content
VM454:334 main: writing frontmatter and body to .md file with fs.
VM454:264 writeToFile: starting
VM454:265 writeToFile: parameters:
tag:
axiom
title: TEST
frontmatter:
---
tags:
  - axiom
id: 20231114201134
subject: "[[Abstract Algebra]]"
reference: "[[Calculus with Analytic Geometry]]"
alias: "TEST"
---

body:
### Fact:

VM454:294 writeToFile: choosing  Atomica
VM454:296 writeToFile: writing to file with fs.writeFile
VM454:299 writeToFile: axiom file written in Atomica
VM454:336 main: content written
VM454:337 main:
 Atomica
VM454:341 main: completed

Nothing in the Templater API documentation jumped out at me. Any insight or suggestions are welcome. Thanks!

After doing some more research, I found this post and this comment which gave me hope, but it seems the solution wasn’t straightforward.

It turns out the issue was with my use of the fs module. While it did play well with Obsidian, it didn’t play well with Templater. What I’ve ended up doing is use the Metadata Menu API to generate and update the frontmatter. Adding the body will be trivial using tp.file.cursor_append.

Now to debug. I’ll update the code on my Github for anyone interested.

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