Many people in a room working together on computers, with a few people talking to each other in the foreground

What are Dynamic templates in Obsidian and why you should use them

Dynamic templates are the way to go if you’re looking for a more efficient way to manage your templates in Obsidian. With this templating strategy, you can bind a template with the current folder, so that if you trigger the dynamic template, the relevant template will be inserted automatically. This saves you from having to think about which template you need and allows you to use a single hotkey for multiple templates.

With dynamic templates set up, you can move your templates into more relevant notes – like canonical notes described in the Context-subject framework – for even greater efficiency.

Why Dynamic Templates

First, dynamic templates are a great way to manage your notes. This templating strategy can bind a template with the current folder. So if you trigger the dynamic template, and the note you are working in is in Folder A, then Template A is inserted. Meanwhile, if the note is in Folder B, then Template B is inserted. This allows you to use a single hotkey(keyboard shortcut) for multiple templates and prevents thinking about which template you need.

In short, dynamic templates give users more control over their note management while providing an easy way to insert commonly used templates into their documents using keyboard shortcuts.

How it works

Imagine you’re working in a note named “My Vacation Plans” stored in a folder named “Travel.” You trigger the dynamic template code, and it starts searching for templates.

First, it searches for a section named “template” within the current note. If it exists, it renders it and returns its content as the template. In this case, there is no section named “template” within the current note, so it moves on to the next step.

Next, it searches for a note named “+template” in the same folder as the current note. If such a note exists, it returns the entire file as the template. In our example, there is no “+template” note in the Travel folder, so again we move on.

The third step is to search for a note with the current folder name, known as the “Canonical Note.” So in our example, obsidian would look for a note named “+Travel” inside of which there might be a section called “template.” If such a file and section existed, that would be returned as our template. But since neither do we continue to step four.

The fourth step is to check the parent folder for a “Canonical Note.” So in our example, since the Travel folder is inside of a folder called “Plans”, the dynamic template code would check to see if there was a “+Plans” note which contained a section called “template.” But alas, there is not, so we arrive at our final step.

The last step is simply to return a message that no matching template was found.

However, if a template existed in any of the above locations, it would be returned by the dynamic template code.

What you need to get started

  1. You need to install the Templater plugin to use dynamic templates.
  2. The dynamic template code (below) can be copied & pasted into your template file. This should be in the same folder specified by your Templater plugin configuration.
  3. I recommended naming the template file something with a preceding underscore, like _insert dynamic template, so that it appears first in your list of templates.
  4. Now you can start moving your templates into more relevant notes, like the “Canonical note” described in the Context-subject framework.
<%*
let render_dynamic_template = async function () {
	let dtemplate;
  if (tp.file.content.contains("# template")) {
    dtemplate = (await tp.file.include("[["+tp.file.title+"#template]]"));
    return dtemplate.remove_template_header()
  }
  const context_folder_name = tp.file.folder(false);
  if(tp.file.exists(context_folder_name+"/+template")) {
    dtemplate = (await tp.file.include("[["+context_folder_name+"/+template]]"));
    return dtemplate
      .remove_template_header()
  }
  const context_doc_name = has_context_doc();
	if (context_doc_name){
		dtemplate = (await tp.file.include("[["+context_doc_name+ "#template]]"));
    if (dtemplate.contains("# template")) {
			return dtemplate
        .remove_template_header()
		}
  }
  const context_group_doc_name = has_context_group_doc();
  if(context_group_doc_name){
    dtemplate = (await tp.file.include("[["+context_group_doc_name+ "#template]]"));
		if(dtemplate !== tp.file.content){
			return dtemplate
        .remove_template_header()
		}
  }
  return "No matching template."
  function has_context_doc() {
    let context_doc_name = context_folder_name;
    if(!tp.file.exists(context_doc_name)){
      context_doc_name = "+" + context_doc_name;
    }
    if (tp.file.exists(context_doc_name)) {
      return context_doc_name
    }
    return false
  }
  function has_context_group_doc() {
    let parent = tp.file.folder(true).replace(/\/[^/]+/, "");
    if (!tp.file.exists(parent)) {
      parent = "+" + parent;
    }
    if (tp.file.exists(parent)) {
      return parent
    }
    return false
  }
}
String.prototype.remove_template_header = function() {
  return this.replace(/^#+ template\s*/, "")
}
-%>
<%_ await render_dynamic_template() _%>

Dynamic templates are extremely useful for managing your notes, allowing you to easily bind a template to the current folder. This prevents you from thinking about which template you need and allows you to use a single hotkey for multiple templates.

Dynamic templates are worth considering if you’re looking for a way to make your note-taking more efficient and organized.

Leave a Comment Cancel Reply