Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Mechanism to share functions/variables across template files

Open ankushg opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. I've defined a bunch of helper functions and common variables to use across multiple templates to reduce verbosity. They frequently rely on tp.file and tR.

In order to have access to them in each template, I currently need to copy/paste the definitions into a <%* %> block

Extracting the helper functions and constants into a separate file and using <%* tp.file.include %> doesn't let me use variables/helpers defined in the external file because the included file logic gets run after the entire main file logic.

Describe the solution you'd like

Something like a "startup script" feature to let me centralize helper function and variable definitions where I still have access to the tp and tR objects from Eta

alternatively

An option for include to run the logic from the included file before the current file

ankushg avatar May 04 '21 18:05 ankushg

An option for include to run the logic from the included file before the current file

Or after, would also be nice.

Use case: Possibly "live" include code/template snippets and have them use the local (including) notes’ front matter data. Thus the "after".

Moonbase59 avatar May 24 '21 08:05 Moonbase59

Templater creates an instance of the tp object when you run a script. That object is accessible at all times and by all files until script execution ends. Write anything you need to access across all includes to the tp object (as in tp.mystring = "hello world").

McGlear avatar Jun 08 '24 17:06 McGlear