SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.27k stars 198 forks source link

Remember and reuse data from prompt #579

Open pcause opened 2 years ago

pcause commented 2 years ago

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

AB1908 commented 2 years ago

Are javascript template commands insufficient for this?

pcause commented 2 years ago

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.

AB1908 commented 2 years ago

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

benwalio commented 2 years ago

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.

pcause commented 2 years ago

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

benwalio commented 2 years ago

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)

Zachatoo commented 1 year ago

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) %>