[Templater] "template inheritance" seems broken

What I’m trying to do

So, the idea is to have separate base templates to use in another templates on the basis of which regular notes are created.
The problem is that when I try including variables definitions and its usages from separate files, the second part comes undefined.

For example,
I have a definition of a variable in one place like this:

<%*
let type_of_thing = await tp.file.folder()
-%>

and its subsequent usage like this:

type: <% type_of_thing %>

And then I unclude these parts into a final template that would be used by a templater to make a note. So, the second part says that the type_of_thing is undefined.

Things I have tried

First idea – using ![[file]] – doesn’t work as templater does not recognize the text as a template.
Second thought – using tp.file.include. I tried all the variations (file, section, block), with no avail. Every configuration leads to a perculiar result – the first part with definitions executes fine, but the part where usage happens fails.

So, it seems like the files are included before the parsig happens, but in a rather odd fashion.

Would appreciate any help!

You might be able to achieve some of the stuff you want by using a user function, and letting that return a object with the variables you want to reuse. All of the tp.file.include() and the ![[...]] will just return executed templates, and as such you can’t use them to define new variables.

So if you defined a user function in _js/Templater/base.js with content like this:

async function base(tp) {
  return {
  "type_of_thing": tp.file.folder()
  }
}

module.exports = base
}

Then you should be able to in your “main” template do stuff like this:

<%* util = await tp.user.base(tp) _%>

<% util.type_of_thing %>

Code untested, but it shows the gist of the idea.

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