Youâre almost all the way to a user function, so good for you. Whatâs a little important to understand now is that your first error message was in the included file, but the last one is in the file you included from.
In that file, there is no definition of the varDateAll
, as itâs not in the same scope. To âtransferâ values between scopes, you need to use functions and return values, and this is where the user functions of Templater enters the scene. So bare with me for the setup part, and I think youâll get the gist of it rather soon.
Setup for user functions in Templater
You need to have a folder for the javascript files, which will become your user functions. Make a dedicated folder for your scripts within the vault, and call it whatever you like, i.e. Scripts
. After creating it, enter this folder name into Settings > Templater > Script files folder location (close to the end).
Now you need to use an external editor to create a javascript file in this folder, and it makes it a whole lot easier if you keep the name the same for all of this. So say you want the getDateAll
function, youâll create the file getDateAll.js
, and for starters itâll hold this text:
function getDateAll(tp) {
... to be filled out ...
}
module.exports = getDateAll
This pattern youâll repeat for every function you create, where youâll change the name of the function, and the tp
parameters of your function (or just leave it out, all together). Iâve included the tp
, as it makes the next step just a little bit easier.
Build your function
The next step is to code your function, and in your case itâs almost a pure copy-paste from the included file, so now your function should look something like:
async function getDateAll() {
let varYear = await tp.system.suggester(["đ today", "2023", "2022", "2021"], ["1", "2023", "2022", "2021"], false, "Year...");
let varDateAll = "";
if (varYear == "1") {
varDateAll = tp.date.now("YYYY-MM-DD");
} else {
let varMonth = await tp.system.suggester(
["Jan", "Feb", "Marz", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"],
["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"],
false, "Month...");
let varDay = await tp.system.prompt("The day...?");
varDateAll = varYear + "-" + varMonth + "-" + varDay;
}
// let vartime = await tp.system.prompt("Which time...?");
return varDateAll
// return [varDateAll, vartime]
}
module.exports = getDateAll
The only thing Iâve done to your code, is (temporarily) remove the start of getting the time. And Iâve commented out a potentially new return
variant, when/if you re-add the time part. Youâll then comment out the other return
, as the function does exit on the return
statement.
Update 2022-12-30: Since the function uses await
, the entire function needs to be declared as async
, and calls to the function should use await
when calling.
Using your script
The new way youâll call your script now depends on whether youâll want to use the value for something else, or if you just want to present it out into your markdown file. This will replace your original tp.file.include()
.
<% tp.user.getDateAll(tp) %>
<%* const varDateAll = await tp.user.getDateAll(tp)
... do something more with the varDateAll ...
%>
Finally, if you re-add the time, and donât include it into the same variable, but use my suggested return, you could get both values using this syntax:
<%* const [varDateAll, varTime] = await tp.user.getDateAll(tp)
... do something with those variables ...
%>
Update 2022-12-30: Due to our function now being async
, itâs needed to call it with await
to ensure it completes itâs statements within.
Youâre of course free to name the script folder and javascript files as you like. But you need to keep the filename the same as the function name, and the same as the end of the module.exports
. And the filename must end in .js
, as in getDateAll.js
. (Hence, the need to use an external editor (or to install some plugins to allow you to edit/create javascript files from within Obsidian))
Hope this helps, and you understand the âhard tobaccoâ of Templaterâs user functions.