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

You can also filter off a key symbol in the file name or even the path. I think this snippit came from @tallguyjenks sample vault a while ago. But it could easily be adapted to trigger off path instead of the first character.

---
<%* if (tp.file.title.charAt(0) == "{") { %>
<%-tp.file.include("[[Templates/Inputs/Book]]")%>
<%* } else if (tp.file.title.charAt(0) == "@") { %>
<%-tp.file.include("Templates/Inputs/Person")%>
<%* } else if (tp.file.title.charAt(0) == "!") { %>
<%-tp.file.include("[[Templates/Inputs/Tweet]]")%>
<%* } else if (tp.file.title.charAt(0) == "%") { %>
<%-tp.file.include("[[Templates/Inputs/Podcast]]")%>
<%* } else if (tp.file.title.charAt(0) == "+") { %>
<%-tp.file.include("[[Templates/Inputs/Youtube]]")%>
<%* } else if (tp.file.title.charAt(0) == "(") { %>
<%-tp.file.include("[[Templates/Inputs/Article]]")%>
<%* } else if (tp.file.title.charAt(0) == "&") { %>
<%-tp.file.include("[[Templates/Inputs/Paper]]")%>
<%* } else if (tp.file.title.charAt(0) == "=") { %>
<%-tp.file.include("[[Templates/Inputs/Thought]]")%>
<%* } else { %>
<%-tp.file.include("[[Templates/General]]")%>
<%* } _%>
---

Whenever I see blocks with so much repetitions, it just screams out to me to make it better through the use of a dictionary where you find the file to include with the character as a key. Similar to how we selected a given set of fields to include a few posts back.

1 Like

Linked method with tutorial above.

  • For the record.

Could this be more readable?

<%*
const choices = [
	{ name: "Books",   char: "{" }, 
	{ name: "Person",  char: "@" }, 
	{ name: "Tweet",   char: "!" }, 
	{ name: "Podcast", char: "%" }, 
	{ name: "YouTube", char: "+" }, 
	{ name: "Article", char: "(" }, 
	{ name: "Paper",   char: "&" }, 
	{ name: "Thought", char: "=" }, 
]

for (var i=0; i < choices.length; i++) {
	if (await tp.file.title.charAt(0) == choices[i].char) break
}

if (i == choices.length) {
	new Notice("Character not found in title")
	return
}

let choice = choices[i].name;
new Notice(choice);

switch(choice) {
	case "Books":
		// insert templater code here
		break
	case "Person":
		// insert templater code here
		break		
	case "Tweet":
		// insert templater code here
		break
	case "Podcast":
		// insert templater code here
		break		
	case "YouTube":
		// insert templater code here
		break		
	case "Article":
		// insert templater code here
		break	
	case "Paper":
		// insert templater code here
		break		
	case "Thought":
		// insert templater code here
		break		
	default:
		tp.file.include("[[Templates/General]]")							
}
tR += choice

_%>

The JavaScript switch might not be necessary if you define your frontmatter inside choices. Fox example, adding a key fields to the dictionary/object. There are two options: (1) adding the frontmatter as a list of properties like @holroy suggested here:

fields: [
    "aliases: ",
    "type: ",
    "tags: ",
    "status: "
]

or, (2) entering the properties, one per line, in a string, between backticks:

fields: `
    aliases: 
    type:
    tags: 
    status:
`
1 Like

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