Is there a way to share variables between template blocks? #232
-
I already opened an issue about this a week ago, but since those don't seem to be as active I thought I'd try my luck here as well: I came up with the following template to create Anki card notes for the Obsidian_to_Anki plug-in: <%*
const decks = await app.vault.getAbstractFileByPath("Flashcards")?.children?.filter(f => f.children).map(d => d.name)
const deck = await tp.system.suggester(decks, decks)
const title = await tp.system.prompt("Title")
await tp.file.move(`/Flashcards/${deck}$/${title}`)
await tp.file.rename(`${title}`)
%>
TARGET DECK: // wanna use `deck` from above here
START
Basic
Front: // wanna use `title` from above here
Back: <% tp.file.cursor(1) %>
END I get the deck it's supposed to go in as well as the title upfront. I use both to move the note to the correct location. And then I want to use both later on in the note again. I marked these spots with comments above. Is there a way to do that currently, without having to get them again? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @jonasmerlin, if the template blocks are in the same template file, then you can re-use the If you look at the doc (https://silentvoid13.github.io/Templater/docs/commands/execution-command), you can do things like that: <%*
function log(msg) {
console.log(msg);
}
%>
<%* log("Title: " + tp.file.title) %> i.e. define a function in a block and use it in another. This works for variables as well of course. Since you want to output something using an execution command ( <%*
const decks = await app.vault.getAbstractFileByPath("Flashcards")?.children?.filter(f => f.children).map(d => d.name)
const deck = await tp.system.suggester(decks, decks)
const title = await tp.system.prompt("Title")
await tp.file.move(`/Flashcards/${deck}$/${title}`)
await tp.file.rename(`${title}`)
%>
TARGET DECK: <%* tR += deck %>
START
Basic
Front: <%* tR += title %>
Back: <% tp.file.cursor(1) %>
END |
Beta Was this translation helpful? Give feedback.
Hey @jonasmerlin, if the template blocks are in the same template file, then you can re-use the
deck
variable in another block.If you look at the doc (https://silentvoid13.github.io/Templater/docs/commands/execution-command), you can do things like that:
i.e. define a function in a block and use it in another. This works for variables as well of course.
Since you want to output something using an execution command (
<%*
) you have to use thetR
variable as specified in the documentation.In your use case, this would give something like that: