Insert Templater Template via a button using Meta Bind

I currently have this button in my “Standups” note and it does what I want - it inserts a template (contains today’s date and a few headings):

name Add a Day
type line(32) template
action Standup Day
templater True

I’d like to replicate the behaviour using Meta Bind. I’ve considered the following approaches but none give me the result:

  1. Use the templaterCreateNote action. This creates a separate note. I’d like the button to insert the template at line X.
  2. Use the Command Action to bring up the command palette and Input Action to select the “Templater: Open Insert Template modal” action (like in the example here). However, I’m not sure if and how I can send the “Enter” key as an action.
  3. Use the Run JavaScript Action and a script to trigger inserting a templater note, but I can’t seem to figure out how to insert text at line X using JS Engine.

Has anyone done something similar or has any ideas what else to try?

I don’t think it’s currently a feature for any meta bind buttons to insert stuff other than at the current cursor position. So maybe you should post a feature request, or consider moving the button to that line? (The latter could possibly be done using an inline button if you don’t want that part of the document cluttered with button definitions)

If using templaterCreateNote, could it be an option to let it create (and/or replace) a given note, and then embed that note at line 32?

If using the command action, instead of opening the modal, use the Templater > Template Hotkeys section to create a command specific for you template of choice, and use that command in the command action. Given a template “_templates/xTemplate.md”, the command created is “Templater: Insert _templates/xTemplate.md”.

If using javascript in any incantations, you can use editor.getLine() and editor.setLine() after getting the current view. See Editor - Developer Documentation for a class reference to this part of the API. It does require some coding though… :slight_smile:

Did you end up finding a solution or opening an RFE against Meta Bind?
I am trying to switch over from Buttons myself because of the bug with including tp. commands in in-line buttons, but it’s bugging me that I can’t have the date be part of the text I’ve got the button inserted automatically.

Here’s one workaround. I can’t find a way to pass information to the template to be used, so I had to rely on a templater system prompt to get the meeting name filled out as an example. This will need the JS Engine plugin that is mentioned on the meta-bind page.

Here’s my template demo note called demo-template-replace:

<%* 
const meetingName = await tp.system.prompt("Meeting")
const date=new Date();
const today=date.getFullYear() + "-" + (date.getMonth() + 1).toString().padStart(2,"0") + "-" + date.getDate().toString().padStart(2,"0");
-%>
# Metadata
meetingName:: <% meetingName + " - " + today %>
type:: standup
attendees:: 

All it does is ask for the meeting name, then will create note contents with the header and the inline fields, with meetingName filled out. You can add other fields like date, etc as needed.

In my daily note, I have the following:

meta-bind-js-view
{frontmatter^meetingTemplate} as fmMeetingTemplate
---

let meetingTemplate="90 - Templates/10 - Notes/demo-template-replace.md";
if(context.bound.fmMeetingName != undefined) {
	meetingTemplate=context.bound.fmMeetingTemplate;
}

lastLine=app.workspace.activeEditor.editor.lineCount()

const buttonString = `
~~~meta-bind-button
label: Create Meeting at line ${lastLine}
hidden: false
title: "Create Meeting"
class: "button-43"
tooltip: "Create Meeting"
id: ""
style: primary
action:
  type: insertIntoNote
  line: ${lastLine}
  value: ${meetingTemplate}
  templater: true
~~~
`
return engine.markdown.create(buttonString);

This renders like this:

If you click the button, it will insert the template at the last line of the file, determined programmatically. When it does this, you’ll get a popup asking you to input the meeting name, and then the text appears at the bottom as desired.

Prompt:

Result:

My meta-bind-js code by default uses a hardcoded template file, but allows for a frontmatter property meetingTemplate to override it if you want to choose a different template at any point without modifying the code.

If you want the style I’m using for the class property, the CSS for it comes from here. This is the style I added to my CSS snippets to style my buttons:

.button-43 button {
  background-image: linear-gradient(-180deg, #37AEE2 0%, #1E96C8 100%);
  border-radius: .5rem;
  box-sizing: border-box;
  color: #FFFFFF;
  display: inline-flex;

  justify-content: center;
  padding: 1rem 1.75rem;
  text-decoration: none;
  width: auto;
  border: 0;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.button-43 button:hover {
  background-image: linear-gradient(-180deg, #1D95C9 0%, #17759C 100%);
}
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.