Auto Select Template on Create New Note #320
rapatel0
started this conversation in
Templates Showcase
Replies: 3 comments 9 replies
-
two questions:
|
Beta Was this translation helpful? Give feedback.
5 replies
-
Beta Was this translation helpful? Give feedback.
1 reply
-
Here's my "New File" template, which gives you a choice of all templates in the defined
<%*
// Helper function, decides which templates to ignore.
// Adjust this function to match your setup.
const ignoreTemplate = name =>
name.startsWith('Part ')
|| name.startsWith('Script ');
// Auto-Detect the templates folder.
const plugins = app.setting.pluginTabs
.filter(tab => 'Templater' === tab.name);
if (1 !== plugins.length) {
new Notice('[Error] Could not find the Templater settings')
return;
}
const templatesFolder = plugins[0].plugin.settings.templates_folder;
// Find all ".md" files inside the templates folder.
const templates = app.vault
.getFiles()
.filter(file => 'md' === file.extension
&& file.path.startsWith(templatesFolder)
&& ! ignoreTemplate(file.basename)
)
.sort((a, b) => a.name < b.name ? -1 : 1);
// Ask the user to select a template. ESC is also allowed.
const templateNames = templates
.map((file, i) => `[${i+1}] ${file.basename}`);
const useTemplate = await tp.system.suggester(
templateNames,
templates,
false,
'Choose a template for the file'
);
if (useTemplate) {
%><% tp.file.include(useTemplate) %><%*
}
%> |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is a default new-note template that will automatically select from other templates when clicking on a link. The idea is that if you have regular notes with a "prefix" then you can simply click on the link with the prefix and it will automatically select the correct template (if you configure it). Whichever link you hit, the appropriate template will be executed as well
if you want to add additional templates, then simply add to the if...else.. block with other prefixes or other selection conditions
Note: this requires the "trigger templater on new file creation" option in templater settings
Beta Was this translation helpful? Give feedback.
All reactions