How to make a single template to fit multiple purposes with templater

It seems like you’re doing this in a rather convoluted way. Why do you define loads of stuff, which you then need to delete or change later on? Why not just define what you need when you need it?

I’m imaginening that your template should rather look something like:

<%*
// Define my document types
const choices = [
  { name: "Minimal",
    fields: []
  },
  { name: "My Code",
    fields: [
      "aliases: ", "type: ", "related:  ",
      "summary:  ", "status:  ",  "rating:  ",
      "url:  ", "folder:  ",
      "partition:  ",  "language:  ", "OS: ",
      'archetype: "[[My Stuff]]"',
      'up: "[[Programming]]"',
      "status: active", 
    ]
  }, 
  { name: "My Computers",
    fields: [
      "aliases: ", "type: ", "related:  ",
      "summary:  ", "status:  ", "rating:  ",
      "assigned:  ", "purchased:  ", "brand:  ",
      "cost: ", "partition:  ",  "OS: ",
      'archetype: "[[My Stuff]]"',
      "tags: personal/assets",
      'up: "[[Equipment]]"',
      'location: "[[Home]]"',
    ]
  }, 
  { name: "My Devices",
    fields: [
      "aliases: ", "type: ", "related:  ",
      "summary:  ", "status:  ", "rating:  ",
      "purpose:  ", "purchased:  ", "brand:  ",
      "cost:  ", "partition:  ",
      'archetype: "[[My Stuff]]"',
      "tags: personal/assets",
      'up: "[[Equipment]]"',
      'location: "[[Home]]"',
    ]
  },
  { name: "My Repositories",
    fields: [
      "aliases: ", "type: ", "author:  ","related:  ",
      "summary:  ", "status:  ", "rating:  ",
      "purpose:  ", "url:  ", "folder:  ",
      "partition:  ",  "language:  ",
      'archetype: "[[My Stuff]]"',
		"tags: content/code",
		'up: "[[Programming]]"',
		"status: unknown"
	 ]
	},
]

// Select document type
const choice = await tp.system.suggester(t => t.name, choices)

// Build frontmatter
tR += "---\n" + choice.fields.join("\n") + "\n---\n"

// Include some templates
if (choice.name !== "Minimal" )
 tR += await tp.file.include("[[Default Minimal Template]]")
else
 tR += await tp.file.include("[[My Stuff Template Test]]") 
_%>

The logic here should be easier to follow instead of defining loads of stuff to delete afterwards, which hides what’s really happening. The outcome (when all entries are defined) should be the same in either case, but now you don’t need to use app.fileManager.processFrontMatter(). That is a tool which I preserve for adding templates or doing stuff after the file has been created, and I want to rework some issues.

2 Likes