How to use a Templater template to select another Templater template?

I want to use frontmatter properties to assign each of my Obsidian notes to one or more categories and subcategories.

For example, I might have a note categoried as science and subcategoriezed as biology:

---
categories: [science]
subcategories: [biology]
---

I want to use a Templater script to implement this. The script would start by asking me what category I want to use for the note:

The script might look like this sample version:

categories: [<% await tp.system.suggester(
[
  "apps",
  "books",
  "computation",
  "entertainment",
  "history",
  "OTHER"
]
,
[
  "apps",
  "books",
  "computation",
  "entertainment",
  "history",
  "PLEASE-FILL-IN-TOPIC"
]
) %>]

So far, so good.

My problem comes next. I want the script to include another templater script based on the category. That script would ask me to select from a list of subcategories. Here’s some pseudocode:

if categories contains "apps"
	tp.file.include("yaml-choose-app")
else if categories contains "books"
	tp.file.include("yaml-choose-books)
else etc.

I have created a number of include files that work fine. But I don’t know how to write the if-statement.

I would really appreciate any help you folks can give me.

BTW, I’ve taken kepano’s advice to heart and used plural property names and list types as much as possible.

Hi, I can’t test anything right now, but might something like this help?
You have to create it in a .js file though and call it with <% tp.user.xxx(tp) %>

async function category(tp){
	const selectedOption = await tp.system.suggester(
		["apps", "books", "computation", "entertainment", "history", "other"],
		[1, 2, 3, 4, 5, 6]
		false
		"?"	
		);

	if (selectedOption === 1) {
		return await tp.file.include("[[Template-apps]]");
	} else if (selectedOption === 2) {
		return await tp.file.include("[[Template-books]]");
	}
...
...

}

module.exports = category;
1 Like

That looks beautiful! Can’t wait to try it. Thanks so much!

When do you call this script? At the creation of a new note? After creating a new note and filling in the categories?

You can also execute javascript code within the template itself, without going through a separate javascript file, by using the javascript blocks, <%* ... %>. Although something like this could be a user function within Templater.

After choosing the sub-categories, what kind of templates do you then want to insert? Just frontmatter stuff or full blown note templates?


There has recently been a few threads related to “meta template picker” which might do some of what you want, but to give precise answer we need a little more details as I’ve asked for above. That would help us give you precise answers.

1 Like

Tell me if it works! I have reused a datepicker script that works. I suck with coding so I’m actually interested if this managed to help.

The way I understood it was:

  1. You open the main template and you select your category.
  2. This then triggers the include function, inserting the subcategory template with a suggester function in the “Subcategory:” property.

This is your first template:

After you select [[Template-apps]], for example, it inserts the apps template, which would be something like:
image

When you have the Other category selected you just replace the tp.system.suggester with tp.system.prompter

Have I gotten it right?

Oops! Just got back from a long trip. I’ll look at this as soon as I can. So much help here!

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