Templater Example Template not working

Hi everyone,
I freshly installed Templater and tried the Quick Example in the introduction of the documentation and it does not work.
Instead, it results in

<< [[<% tp.date.now("DD.MM.YYYY", -1) %>]] | [[<% tp.date.now("DD.MM.YYYY", 1) %>]] >>

# <% tp.file.title %>

<% tp.web.daily_quote() %>

Trying another example yields

modification date: <%+ tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss") %> // This doesn't currently work in front matter, hoping that gets fixed.

I found a very similar topic in this forum:
Templater not working what's wrong?.
But the authors solution does not speak to me and I can not reply to it, because it’s already closed.

Hope to find some help here.
Happy new year!
Carl

I found myself the problem…
To all of you with the same problem and, perhaps, to my future self:
Templater will not execute commands on the fly. It needs to be triggered to replace commands with Markdown code.
The solution is to execute Templater during file creation. There is a respective toggle switch in the preferences of Templater.
Another option is to use the the hotkeys for file creation (e.g. daily notes) and Templater. I guess this is what my predecessor meant in Templater not working what's wrong?

Hope this helps. If possible, it would be helpful to merge this topic with the one mentioned above.

Best
Carl

1 Like

There is more to be said on this subject, which might not be very clear from the documentation.

Various ways to insert templates

One way to trigger Templater is as you’ve discovered through applying/inserting the template when you create a file.

However, you can also insert a template into any file open for editing, either through the command: Templater: Open Insert Template modal, where you select which template to insert, or through Template Hotkeys-commands.

I’ve added a hotkey for my custom command: Templater: Insert _templates/zTemplate.md. This command was added from Settings > Templater > Template Hotkeys. There I selected my _template/zTemplate.md, and later on assigned a hotkey to this template. This allows me to hit that hotkey, and insert that template whenever I want into the active file.

Through plugins

Yet another way to trigger templates, is through other plugins, like the buttons-plugin, where you can do stuff like:

```button
name time
type append template
action Now_Template
```

Which could produce output like the image below when you hit the button:
image

Different run variants of templates

In addition to these ways to insert the templates, there are even more ways to trigger the Templater engine. Try adding the following to a file:

%: <% tp.file.last_modified_date() %>
%+: <%+ tp.file.last_modified_date() %>
%\* : <%* tp.file.last_modified_date() %>
%\* tR:  <%* tR += tp.file.last_modified_date() %>
%\*+ tR:  <%*+ tR += tp.file.last_modified_date() %>

If I goto the reading view for this file without doing anything else, I get:
image

Here you can see that the <%+ and the <%*+ tR lines are actually executed, and produces the last modification date of my file. Other Templater code can be executed the same way. So the + makes it a dynamic template, where the result is shown in the reading view. Note that this is a non-destructive way of triggering the Templater engine, as the code in the file stays the same.

The final way, that I know of, to trigger the Templater engine, can be done with the same file, by executing the command: Templater: Replace templates in the active file. If I do this in the file mentioned above (after removing the last line, %\*+ tR, which triggers an error in this case), it replaces (aka destructive editing) the templates of the source file, with:

%: 2023-01-02 18:04
%+: NaN
%\* : 
%\* tR:  2023-01-02 18:04

Here you see that it executes the various templates, and replaces text accordingly to its normal rules. So the first and the fourth (remember the last needs to be removed for this to work), do get the actual last modification date of the file (when the command ran). The second has a little strange response (as it is meant to produce output in the reading view). The third, doesn’t produce any output, since the tR variables wasn’t changed.


I hope this was illuminating, and food for thoughts on various ways to utilise the Templater plugin. Some documentation on this last part on various incantations of the Templater engine, can be found at Commands - Templater.

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