Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Remember and reuse data from prompt

Open pcause opened this issue 2 years ago • 7 comments

Is your feature request related to a problem? Please describe. I have a template that creates an item and also includes a todo. I want an item title to be prompted for and then be able to reuse the value entered as the text of the todo.

Describe the solution you'd like Add another argument to the prompt function called "variable" which is a string and whatever is entered can be remembered as that variable. Then allow that to be inserted vi tp.variable(), a new function which looks like:

tp.variable(type:set}getm name:string, value?:string)

Describe alternatives you've considered Javascript, but I wanted something simple and not as wide open as enabling javascript

Additional context

There might be other functions where being able to reference a variable is useful

pcause avatar Mar 03 '22 20:03 pcause

Are javascript template commands insufficient for this?

AB1908 avatar Mar 06 '22 15:03 AB1908

Yes, I could do this in javascript. My thought is that not all users know javascript so for those who don't and can use at least things excel formulas something like this can work. Not trying to turn this into a full blown alternate programming language. More like what task or datavue do in their code blocks but fit into as templater style.

pcause avatar Mar 06 '22 16:03 pcause

Javascript is natively supported in the plugin but your abstraction sounds useful.

AB1908 avatar Mar 06 '22 22:03 AB1908

Just adding in, I've been working on this using javascript although the abstraction also sounds nice.

<%* 
let prompt = tp.system.prompt("New Prompt")
	.then(p => {
		return p
	}
)
%>

# <%* tR += await prompt %>

<% tp.file.rename(await prompt) %>

The above seems to work fine. Since we're adding in a .then() we have to treat the prompt var as a Promise and await it. I'm not sure it's entirely necessary, but I was doing a few other things in that .then() in my tests and just sanitized it for posting here.

benwalio avatar Mar 10 '22 13:03 benwalio

thanks. not clear how I set the return to be used/inserted a second time.

pcause avatar Mar 10 '22 13:03 pcause

I'm not sure I follow, you should be able to use await prompt in other areas. I have it going into a different JS script and that would resolve after prompt resolves. You could also chain .then()s depending on what you're trying to do.

In the above example, I have it in two places, to set the H1 and then to rename the file.

if you wanted to add the prompt var into a task in the body, it would look something like this:

<%* 
let prompt = tp.system.prompt("New Prompt")
	.then(p => {
		return p
	}
)
%>

# <%* tR += await prompt %>

- [ ] TODO: <%* tR += await prompt %> 

<% tp.file.rename(await prompt) %>

(i haven't fully tested the above as i'm at work, but it should work)

benwalio avatar Mar 10 '22 15:03 benwalio

For those that find this in the future, we can make it look a little prettier.

<%*
let prompt = await tp.system.prompt("New Prompt")
-%>

# <% prompt %>

- [ ] TODO: <% prompt %>

<% await tp.file.rename(prompt) %>

Zachatoo avatar Sep 23 '23 00:09 Zachatoo