Rendering MustacheTemplate example

I’m having trouble getting started with the MustacheTemplate example script found in the Drafts Script Reference guide. I have plugged the example into a Script step and the script runs successfully (green success notification), but no new drafts are actually created.

I apologize if I’ve overlooked something simple, but I can’t see why Drafts isn’t returning new drafts based on the template embedded in the script. I tried changing the “Draft.create()” function in the script to “new Draft(),” but that didn’t make a difference. I’ve also studied Mustache Prompt, but I can’t discern what’s different there in terms of rendering the template — both seem to use MustacheTemplate.createWithTemplate() and template.render().

I appreciate any guidance. Thanks!

The example you are referring to renders the output to a result variable in the last line. It creates two temporary drafts (d1 and d2) as an example of how to pass data to the template, but does not actually create any drafts or do anything with that output.

If you wanted the output of the rendering saved in a draft, you could add this at the end:

let d = new Draft();
d.content = result;
d.update();

That worked to create a new draft in the Inbox. Thanks!

But … what the script created was just one draft that contained both the contents of variables d1 and d2 (“First draft / Flagged!” and “Second draft / Not flagged”). Is the script supposed to be creating two separate drafts? I’ve tried a couple different kinds of for loops but I only seem to be succeeding in creating two of the above drafts, not two separate drafts with different content.

Again, I’m sure the problem is I’ve misunderstood something. Thanks very much for your help!

The example demonstrates how you can pass variables to render in a template and output a string. The use case is something like outputting drafts returned from a query into some sort of report/summary/table of contents, or similar.

Templates (Mustache or otherwise) are just about transforming a string into another string while inserting some values in the string.

Is there something particular you are hoping to accomplish with Mustache templates, or just exploring what’s available? Would be happy to guide you in the right direction if you have details on what you are hoping to build.

1 Like

Thanks again — with your guidance I got this worked out. I had wanted to be able to create a JSON document in Drafts and then run a script on it to take the values and enter them in a template (where the template variables were the JSON keys and the substitutions were the values).

As I understand it, the same thing could be done using the Prompt action, but for my purposes I wanted to be able to add the value information to the JSON document over a couple of days and then run the script on it, rather than having to enter all the information at once via a prompt. I’ll essentially be generating two different templates here — one to generate the JSON document with all the keys but the values not yet filled in, and then the final document rendered via Mustache taking the values from the JSON draft.

Again, thanks so much for talking me through this.

Hi.

I was interested in Mustache Template.
In particular, I thought that “Lambdas” had high utility value.

For details on using Mustache templates, we recommend reviewing tutorials.

in MustacheTemplate | Drafts Script Reference. The “tutorials” link results in “Database Error” and cannot be referenced.

Therefore, I tried all the formats written in mustache (5) --Logic-less templates. on Drafts.app.

Drafts action:
MustacheTest | Drafts Directory

Result:


---

### typical Mustache template
Hello Chris
You have just won 10000.0 dollars!

Well, 6000.0 dollars, after taxes.


---

### Variables
* Chris
* 
* <b>GitHub</b>
* <b>GitHub</b>

---

### False Values or Empty Lists
Shown.


---

### Non-Empty Lists

  <b>resque</b>

  <b>hub</b>

  <b>rip</b>


---

### Lambdas

  Willy is awesome.


---

### Non-False Values

  Hi Jon!


---

### Inverted Sections


  No repos :(


---

### Comments
<h1>Today.</h1>

---

### Partials
undefined

---

### Set Delimiter
* default_tags

* erb_style_tags

* default_tags_again

“Lambdas” and “Partials” don’t seem to work properly.
At “Partials”, I put user.mustache (its content is “ {{name}} </ strong>”) in the Library/Templates folder.

Any advice would be appreciated.
Thak you.

See the “About Passing Data to Templates” note in the docs. Only key/values of basic data types, and nested data types are supported.

I understand. thank you.