Use templater API

What I’m trying to do

Use templater functions in a dataviewjs block.

Things I have tried

I tried the code i found on other threads, but it doesn’t seem to work for me.

const tp = app.plugins.plugins["templater-obsidian"].templater.current_functions_object

console.log("app...", app.plugins.plugins['templater-obsidian'])
console.log("tp", tp)

You can see on the image that there’s no current_functions_object under templater.
image

I’m looking for the correct way to access templater API on Templater 2.1.0.

1 Like

This seems to be the correct incantation of tp, but it does depend on Templater having run at least once before in your vault since the last startup of Obsidian.

So try inserting some random template somewhere in your vault, and then check this code again. If it now works, this issue is what has happened to you. To avoid it in the further, there is a simple trick of making an empty template, and set that template as a startup template for Templater when you restart your Obsidian. That way, it would always give the current functions object its value needed for accessing the Templater API from other places.

3 Likes

Thank you! It’s working now.

Another alternative I found for situations like this when one doesn’t want the Templater plugin to have been run at least once:

// get access to the internal modules
let tp = app.plugins.plugins["templater-obsidian"].templater;
tp = tp.functions_generator.internal_functions.modules_array;

// get an instance of the date module
let tp_date = tp.find(m => m.name == "date");

// METHOD 1 of getting access to a specific module function
let tp_now = tp_date.generate_now();

// or METHOD 2 of getting access to a specific module function
// let tp_now = tp_date.static_functions.get("now");

// print the output of that function
dv.el("p", tp_now());
4 Likes

Hello team,

I am running into the same issue here.

I do notice sometimes these class calls do work, and I have seen both instances where current_functions_object is defined and undefined… Assuming that I have somehow ran tp since last startup.

However my issue seems to be when tp tries to construct itself. As it loads the scripts that call for it to use itself…

Right now I have to remove the sub folder that contains my classes from scripts for any of my other template scripts to work. As soon as I add them back in, Community cannot be defined becase it cannot define rObject.

I am trying to expose javascript class modules from the tp.user functions.

scripts/rObject.js

class rObject {
    constructor(props) { ... }
    ...
}
module.exports = rObject;

scripts/Community.js - extends rObject.js

const tp = app.plugins.plugins['templater-obsidian'].templater;

let modules;
let rObject;

if (tp.current_functions_object) {
    modules = tp.current_functions_object.user;
    rObject = modules.rObject;
} else { throw "Error" }

class Community extends rObject {
    constructor(props) { ... }
    ...
}
module.exports = Community;

Am I to the point where I need to build my own plugin or can this be accomplished in some other way?

Have you tried using the CustomJS plugin? Ot potentially utilising the startup template of Templater?

I’m not sure how easy it is to make the user functions trigger in a given order. You might need to either import/require stuff from your rObjects, or possibly you could use alphabetical order to trigger when they’re loaded.

Thank you for the good call out with CustomJs. I have an init invoked class.

Is there a way to Export and Import class definitions between customjs scripts?

Otherwise, what command would we need to run to tell tp to initialize the user functions?

I failed to realise this was an extension on an already answered question. Please repost as a new thread, including what you’ve learned and/or tried so far, @waniel .

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