-
First... Templater is beyond amazing as a plugin for Obsidian. My only concern is that if I want to build some fairly extensive templates with a bunch of custom functions, it appears that I have to create one js file for each function and export that. This can be really messy after a while. I have searched and experimented but I cannot seem to find an easy way to consolidate related functions into a single JS file. For example, let's say I wanted to create 5 different template functions that each produce a different text string to inject. Normally in JS, I would create a simple object with each key being a function and finally export the object. const SomeLib = {
functionOne() {
// does something
},
functionTwo() { },
functionThree() { },
functionFour() { },
functionFive() { }
}
module.exports = SomeLib; Then I would be able to reference the desired function with Is there any way to do something similar in Templater where I can use |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
What I did is create a class and export the class in the .js file. Then, in templater, I create a new class, and refer to functions from that class. filename.js
// filename.js
class SomeLib {
functionOne() {new Notice("functionOne", 5000)}
functionTwo() {new Notice("functionTwo", 5000)}
functionThree() {new Notice("functionThree", 5000)}
functionFour() {new Notice("functionFour", 5000)}
functionFive() {new Notice("functionFive", 5000)}
}
module.exports = SomeLib Template
// Templater Template
<%*
const f = new tp.user.filename;
f.functionTwo();
%> Hope that helps. ~Welp |
Beta Was this translation helpful? Give feedback.
-
@welpdx Interesting solution. Of course, it's the one approach I had not tried. 😏 Thanks for the suggestion. |
Beta Was this translation helpful? Give feedback.
-
@dainbrump, no problem! 😃 |
Beta Was this translation helpful? Give feedback.
-
@SilentVoid13 Unless there is an "official" answer or solution to this, I'll gladly accept the answer @welpdx gave and we can close this. Hopefully this might help others. Now to setup a typescript project to hold all my fancy new user scripts. :-D |
Beta Was this translation helpful? Give feedback.
-
You may also find the CustomJS plugin useful. You can use that library of functions elsewhere like in DataviewJS. |
Beta Was this translation helpful? Give feedback.
-
I think it would be really convenient to export multiple functions from file in object style, may be reopen issue as enhancment? |
Beta Was this translation helpful? Give feedback.
What I did is create a class and export the class in the .js file. Then, in templater, I create a new class, and refer to functions from that class.
filename.js
Template
Demo
Hope that helps.
~Welp