shabegom / buttons

Buttons in Obsidian
The Unlicense
455 stars 47 forks source link

Templater codeblock vanishes and shows "undefined" #133

Closed AlbertoEE closed 5 months ago

AlbertoEE commented 1 year ago

I have 2 buttons on the same note.

button
name Add NPC
<% tp.user.createNewNpc(tp)%>
templater true
button
name Add Session
<% tp.user.createNewSession(tp)%>
templater true

When clicking one of them, both are executed, and then the buttons look like this:

button
name Add NPC
undefined
templater true
button
name Add Session
undefined
templater true
AlbertoEE commented 1 year ago

Okay I will answer myself -> Always return in your javascript function the string with the templater command.

Example:

async function createNewNpc (tp) {
    const folder = app.vault.getAbstractFileByPath("Areas/Gaming/D&D/NPCs")

    let npcName = await tp.system.prompt("Enter NPC name: ")

    let file = await tp.file.create_new(tp.file.find_tfile("NPC"), npcName, false, folder)

    let content = await app.vault.read(file)

    console.log(content)

    return "<%tp.user.createNewNpc(tp)%>"
}

module.exports = createNewNpc;

This is the important part return "<%tp.user.createNewNpc(tp)%>".

Here is the code of the button:

button
name Add NPC
<%tp.user.createNewNpc(tp)%>
templater true
AlbertoEE commented 1 year ago

This only solves on problem. When you click any button on the page it still executes all templater scripts linked to buttons run.

MMoMM-org commented 1 year ago

the code will run the Templater command replace template in note.. and undoes that afterwards. that might be your issue. and I don't know if calling a javascript in that way will work, as you probably need to have await in front of the call of tp.user....