Creating categorized links with Templater

What I’m trying to do

Hello! I would like to have different categories of links (e.g. for literature, primary sources, my ideas, etc.). This would allow me to then assign a specific color to each category, and have a much tidier and visually clear document!

Things I have tried

I am trying to use Templater to do this. I created a Template with this code:

<%*
const category = tp.prompt(“Enter the category (lit, ps, case, top, idea):”);
const title = tp.prompt(“Enter the note title:”);
const prefix = category === “lit” ? “lit_” :
category === “ps” ? “ps_” :
category === “case” ? “case_” :
category === “top” ? “top_” :
category === “idea” ? “idea_” : “”;

const formattedLink = [[${prefix}${title}|${title}]];
%>
<%= formattedLink %>

However, if I try to invoke this template, I receive the following error notification: “Templater error: template parsing error. Aborting. Check console for more information.” This actually happens even time I try to invoke a template with Templater - even a much simpler one.

What am I missing?? I am thinking this may have to do with my Templater settings, or the way I use it. I am very new to this.

To invoke a template I do the following:

cmd + P, select “Templater: Open insert template modal”, and then select the note that contains the code (the note is located in the Templates folder where Templater goes to look).

In terms of settings, ChatGPT tells me I need to toggle on an option to “Enable JavaScript in templates”, but I don’t have that option. All I have is a “Scripts files folder location”, but that is for JavaScript files, not markdown files, which is what my code is. Should I put my code in a Java script instead and place it in the root folder?

Thanks for any help !

I might be wrong, but this could be a configuration issue. So you’ll need to check the following setting: Settings > Templater > User script functions > Script folder locations. This should be a folder within your vaults, which should only contain specific files for Templater user script function.

If this folders has any other files, and that is either other markdown files or some other javascript files, then you might experience that Templater produces error messages even when trying to insert some other random template somewhere else. This folder needs to hold only those specific user script files. If you don’t know what a user script file is, then just clear out the value of this setting. If you got other javascript files in this folder, then you need to create a dedicated folder for the user script files, and update the settings accordingly.

Maybe that’s just a copy/paste thing … but tp.prompt() isn’t a Templater thing :blush: … You’re actually looking for tp.system.prompt
This could explain why Templater is throwing an error.

For the category part, it would potentially be easier to use tp.system.suggester() as if I remember correctly, you could directly link a category to the desired prefix :blush:

Edit: Adding a tp.system.suggester() example :

<%*
// Array storing the categories
const categories = [
	"lit",
	"ps",
	"case",
	"top",
	"idea"
];
// Array storing the prefixes corresponding to the categories
const prefixes = [
	"lit_",
	"ps_",
	"case_",
	"top_",
	"idea_"
];

const title = await tp.system.prompt("Enter the note title:");
const prefix = await tp.system.suggester(categories, prefixes);
const formattedLink = `[[${prefix}${title}|${title}]]`;
-%>
<% formattedLink %> 

This could also work:

<%*
// Array storing the categories
const categories = [
	"lit",
	"ps",
	"case",
	"top",
	"idea"
];

const title = await tp.system.prompt("Enter the note title:");
const category = await tp.system.suggester(categories, categories);
const formattedLink = `[[${category}_${title}|${title}]]`;
-%>
<% formattedLink %>