Is it possible to run a templater user script on a dataview code block?

I am trying to create a button from the Button plugin inside a Dataview table that when clicked will run a templater user script.

const {createButton} = app.plugins.plugins["buttons"];


dv.markdownTable(["", ""], [[
createButton({app, el: this.container, args: {name: "Button 1"}, clickOverride: {click: script, params: [dv.current().file.path]}}),
createButton({app, el: this.container, args: {name: "Button 2"}, clickOverride: {click: script, params: [dv.current().file.path]}})

If there was a way to capture the script’s function like this const {createButton} = app.plugins.plugins["buttons"]; it would be perfect, but I am not sure how to access Templater’s API and it’s scripts or even the tp object. If I had access to tp inside the dataview code block I could simply use tp.user.script(), and that would work as well.

It doesn’t necessarily needs to be a Templater script, all I need is to run a function that I created on a separate file, so I can change the functionally of all buttons on a single place, instead of going to each note that has that button and changing the method. I have no idea how to do it though. Any help and ideas would be appreciated.

I figured it out.
You can get the user functions with:

  • const {FUNCTION_NAME} = app.plugins.plugins["templater-obsidian"].templater.current_functions_object.user;

It is also possible to get other templater functions with:

  • const {TEMPLATER_FUNCTION} = app.plugins.plugins["templater-obsidian"].templater.current_functions_object.TEMPLATER_MODULE;
  • tp.system.prompt()
    • const {prompt} = app.plugins.plugins["templater-obsidian"].templater.current_functions_object.system;

I saw your post a little earlier on, but its been a busy day, so good for you that you found out of it. I first saw the Templater api referenced by @AlanG here: How to calculate my work hours - #8 by AlanG

And there he doesn’t pull out a single function like you do, which indeed is a neat trick, but he rather pulls out the “default” tp object:

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

And then uses the various functions like you do in a template, like doing tp.system.prompt(). I kind of like that approach, but this a matter of personal preferences.

Whilst on a similar topic, I would also like to mention two alternatives which might be useful for other use cases:

So depending on your use case, either of these could be slightly better suited for the task at hand, but I just wanted to mention them to kind of broaden the view of alternatives available.

1 Like

Having the tp object is a much better choice than mine, I will start using it.
Thank you for all the info, it will really help me!

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