How to remove text from the template after it has been executed?

I understand how I can dynamically add text to a (new) note using templates (templater plugin). After all, that is the very idea of templates, right?

But how do I remove text using templater?

I realize there are two ways of interpreting this question. What I want to know right now is the answer to the first interpretation, but I’ll add the second one below.

1. Removing text that was in the template but should not be in the resulting file

Why would I want to do that? Here is my use case (and maybe there is a better way of solving the problem?): Using the Force-view-mode plugin I want make sure that all my templates will always be displayed in source so I’d add this to the yaml-header of my templates:

obsidianEditingMode: source

The problem is: I obviously don’t want this to carry through to every note I create using the template.

Hence my question: is there a way of telling templater that this header should be removed?

2. Removing text that already exists in a note to which the template is applied.

Well, I don’t have a real life example, but let’s say you have a template that inserts a letter default letter-text into the current note. The inserted text starts with “Dear …” so if you maybe already typed “Dear …” before inserting the template, then you’d want the template to remove the line that starts with “Dear …”.

I know, stupid example, but you get the idea.

1 Like

Would it be an option to either use the <%* ... %> or a dataviewjs query to insert the header, only when the current file is within your template folder? Not sure if this would cause a race condition somehow, but it could possibly be worth attempting?

Template is as you’ve stated mostly used for adding new text according to the template. However it does have access to the entire content of the file, and there exists ways to create new/modify files from within Templater, so I reckon it can be done. Whether it’s easy or safe, that’s another question.

Another option could possibly be to have your template insert a button which removes lines (and itself). In this case I imagine that your template calculates which lines to remove, and then insert a button at the end with commands to remove those lines and the button itself.

In other words, I think it can be done, but I’m not sure how easy or safe either option would be. But then again, I’m just this rather new user of Obsidian, and there might even be a plugin or safer methods to achieve your goals.

The easiest way would be to use multiple templates and run one master template witch contains the following code…

<%* let filetype = await tp.system.suggester(["Use Template One", "Use Template Two", "Use Template Three"], ["TemplateFile1", "TemplateFile2", "TemplateFile3"], false, "Which template do you want to use?") %>
<%-* if (filetype === "Use Template One") { %>
<% tp.file.include("[[TemplateFile1]]") %><% tp.file.cursor(1) %><%-* }
 else if (filetype === "Use Template Two") { %><% tp.file.include("[[TemplateFile2]]") %><% tp.file.cursor(1) %><%-* }
 else if (filetype === "Use Template Three") { %><% tp.file.include("[[TemplateFile3]]") %><% tp.file.cursor(1) %><%-* }

This will prompt you to select the template to use. You can set templater to use this master template on file creation in the folder your interested in (use “/” for the root of your vault).

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