Button: new note based on template #2309
-
I want to add a button that creates a new note using an existing template. I thought this should be simple enough but it is my first foray into coding in Trilium (with only limited javascript experience generally) and I am really struggling. I have used the example script for creating a button and am trying to add the 'template' attribute to the newly created using CreateNoteAttribute. The note gets created but the template is not applied. Any help would be greatly appreciated!
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello! Basically your problem is that adding a template to a note means adding a relation, linking your new note to another existing note with a relation attribute. For that, you need .setRelation with the template's noteId. I've done something similar in my Hexmap widget and here's how, modifying your example:
I've changed a few variables names and such when I was testing because it felt more consistent. If you're interested, my two widgets (Hexmap widget and Timeline widget) are on my gitlab with the source code, you can also download the .zip and import it into your own install to test them and see what they do. If you're not already doing it, I also recommend you open the browser console (because trilium is an electron app, meaning it's built on chromium and functions basically like google chrome) with CTRL+SHIFT+i, so you can watch the console for errors and better understand what's going wrong. |
Beta Was this translation helpful? Give feedback.
Hello!
Basically your problem is that adding a template to a note means adding a relation, linking your new note to another existing note with a relation attribute. For that, you need .setRelation with the template's noteId.
Right now in your example, you're creating an attribute of type 'relation' to nothing, because 'template' is just a meaningless string, it's not what a relation attribute requires.
I've done something similar in my Hexmap widget and here's how, modifying your example: