Insert template by adding / updating tag

I want to change this line in my templater meta template (from a bryan jenks tutorial)

<%* if (tp.file.title.charAt(0) == "{") { %>

to instead of looking for the 1st character of the title - { - & applying the book template

to instead look for a tag in the metadata e.g #source/book and then apply the book template

However when i add tp.file.tags to where i think it should be

<%* if (tp.file.tags == "source") { %>

it doesn’t work in the same way as when i was using a preceding symbol in the title?

Can anyone advise what the correct syntax should be, so that when i add or change a tag, and apply the templater template, it adds the book template.

Many thanks

My guess is that (tp.file.tags == "source") is always failing because you’re not comparing like objects. tp.file.tags is an array of strings, while "source" is simple string. Try using (tp.file.tags.includes("source")) instead.

1 Like

Thanks @ninjineer - that makes sense. Tried that & hasn’t seemed to solve it, so think on reflection the issue may lie with my approach, as it requires the tag to be in the file already, and then inserting the template, rather than the template changing automatically by picking it up the tag, which is was hoping for. So maybe one for me to shelve and use the tp suggester instead, which i’ve recently found as a solution that doesn’t require me remembering which prefix identifier relates to which template. Thanks again for your input.

I think it all comes down to what is executed in which order, and regarding new files and templates it seems this is the order:

  • You trigger the new file creation
  • The file is created, either with “Untitled” as the file name, or if you followed a link with that suggested file name
  • The template is called on this newly created file, and it expands the template
  • You’re given control back over the file, and start inserting stuff
  • (Some time later) You can insert other templates…

Since you don’t have any control (disregarding prompts and suggesters from the template) before the template is expanded already, you can’t look at newly (or not) entered metadata and then decide to use the book template.

So your options as I see them are three-folded:

  • You use the templater options of using a given template, given that your new file is created in a given folder
  • You have a suggester within your template, which allows you to choose which template you should apply to the new file. This of course, needs to be at the start of your template
  • You have a generic template for any new file, and then after you’ve regained control over the file, you insert metadata, and then another template, which forms it according to the meta-data

Note however, that the latter option doesn’t really allow you for a full rebuild of the file, it’ll be more of an addon variant of a template. So it wouldn’t be easy to remove or alter sections already presented into the file.

1 Like

Thanks @holroy - that’s really useful.

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