TIP: Creating New Drafts with Templates

UPDATE

  • 2023/11/05: Added “Apply Template” section below with example action for applying template to an existing draft.
  • 2023/03/09: Added “Placeholder” example actions discussion below to support navigation between <|field> fill-in placeholders in the resulting template.
  • 2021/09/01: Updated action to support a “Omit First Line in New Draft” option, that allows you to use a friendly name for the template in the first line, like “Meeting Notes Template” that helps when selecting templates in the prompt, but will no be included in the output.
  • 2020/11/01: The current version of this action has been updated with new features, such as cursor placement, as of Nov. 1, 2020. You may need to reinstall the action from the directory if you have a previous version installed.

It is often handy to create a new draft based on some boilerplate template content to ease the creation of meeting notes, lists, or other content with a repetitive structure. The New Draft with Template example action provides an easy way to do this based on templates maintained in other drafts. Let’s briefly discuss how to use this action to maintain a robust template system in Drafts.

Install the Action

Visit the New Draft with Template action in the Action Directory on a device with Drafts installed. Tap the “install” link to install the action and Drafts will launch and prompt you to complete the install. If you have the action already, you should repeat this and choose to update your existing installed version.

Create Templates

For the “New Draft with Template” action to work, you must first create one or more templates. Do the following:

  • Create a new draft. Put in the text content you want to use as a template for new drafts in this draft.
  • Make sure the first line (title) of the draft is something identifiable, as it is used to identify the template when the action is run.
  • Assign the tag “template” (singular) to the draft.
  • Assign any additional tags desired. When a new draft is created using a template, any tags (other than “template”) will be assigned to the new draft. So, for example, your “Meeting Notes” template might also have a “meeting” tag, so it will automatically be filed correctly.
  • Repeat for any additional templates you wish.

These template drafts can be in the archive or inbox, but I recommend moving them to the archive to avoid cluttering your inbox. Templates can also be flagged. When the action is run, the prompt to select a template sorts based on the text of the draft but also sorts flagged templates to the top of the select list, so if you flag often-used templates, they will appear first.

Using Template Tags

Template drafts can also contain drafts tags. Tags will be evaluated when new drafts are created using the template, so are great for inserting dynamic time stamps. If you are new to using tags in Drafts, this is a great guide.

Run the Action

Once you have created one or more templates, run the New Draft with Template action. You will be prompted to select from a list of all your templates. This list is dynamically generated by finding all drafts assigned the tag “template.” So, at any time you can add to or remove templates from the options simply by adding or removing the “template” tag from drafts.

After you select a template, the action will do the following:

  • Create a new draft with the content of the template
  • Assign any additional tags (and syntax) from the template to the new draft.
  • Load the new draft in the editor, ready to edit.

Fill-in Placeholders

Often, you may have one or more values you want to fill in in a template after creating it. This action, along with the two companion actions below, make it easy to do that by creating fill-in placeholders in the template in the format <|fieldname> (fieldname is optional, <|> will work also, but the field name allows you to notate what to fill-in).

If any placeholders in this format are found in the template after creating a new draft, this action will locate the cursor at the first placeholder, ready for you to type the replacement text.

If you have templates with more than one placeholder, install the sample actions below to get an easy way to navigate through placeholder in a draft to insert new values. When used, they will move the text selection to the next (or previous) placeholder, ready for you to type the replacement text.

  • Previous Placeholder: Find previous <|> style placeholder and select its text. Default keyboard shortcut: control-command-up arrow.
  • Next Placeholder: Find next <|> style placeholder and select its text. Default keyboard shortcut: control-command-down arrow.

An example of use for placeholders might be in a “Meeting Notes” template, where you would have a template like:

Topic: <|topic>
Attendees: <|attendees>

# Notes
<|notes>

After creating a new draft, the draft would be open in the editor with <|topic> as the selected text, ready for you to replace that value. You could then use the next/previous placeholder actions to move the selection to each value to complete the new draft.

Manage Templates with a Workspace

Note: You can create the workspace described below manually or install a sample “Templates” workspace from the directory.

If you plan to use this action, it’s a great idea to create a “Templates” workspace to keep up with your templates so you can easily update them as needed. I recommend configuring the workspace with the following options:

  • Tag Filter: template
  • Archive Sort Order: Text - Ascending
  • Archive - Sort flagged to top: On
  • Select Archive : To automatically switch to the archive when loading the workspace. This allows you to store your templates in the archive, so they do not clutter your inbox.

When you need to edit your templates, you can open this workspace to access them all in one place easily.

Widgets!!!

The “New Draft with Template” action is great for use with iOS Widgets! Add the action to a grid widget on your Home Screen, and you can quickly jump straight into Drafts with a new draft based on one of your templates ready-to-go.

Applying Template to Existing Drafts

Sometimes you realize you want to use a template after you have started on a draft. We also provide an example Apply Template action, which can be used to apply the tags, syntax, and content from one of your templates to the current draft.

When run, this action will prompt to select the template, and instead of creating a new draft, it will:

  • Assign tags from the template to the current draft
  • Assign syntax from the template to the current draft
  • Insert the content of the template in the current draft as follows:
    • If a <|> placeholder exists in your template, the content of the current draft will replace that placeholder in the new content. This allows a template to have content both before and after where the current content is inserted.
    • If no <|> placeholder exists, the template content will be inserted before the current content of the draft.

Summary

With a few simple steps and just one action, you can manage a robust template system in Drafts. Have special requirements? Use this example as a starting point to modify to suit your needs.

23 Likes

This is very useful. Thanks. Just one query. How can I configure the text so that the date is initiated as the current date. I have a TE snippet and it expands. What does [[date]] do? I couldn’t get that part. If you can explain how the current date will be automatically inserted, it would be might useful. Even now it is, but ironing out the date wrinkle will help. Sorry to be sounding completely naive, but I don’t have any knowledge of Java script etc.

1 Like

The drafts tags link above links to information that covers this. It isn’t JavaScript, but a feature built into Drafts itself.

1 Like

This Using Drafts Templates article is the best place to start on how tags work in Drafts. This action runs the template draft through the template engine like it would be if it was a template in an action step, so all the tags will be evaluated when the action is run - in this case the new draft is empty, so it’s really only useful for time stamps.

For using the draft templates, should I change the settings from Markdown to simple text?

This looks very usefl to me. ONly I seem to have missed something becausen I do not know how to take the first step on my iPhone 8

Can you help me with that please
TIA

Actions are installed from the action directory by tapping the “Install” link, see attached. You need to be on your iPhone, in Safari, on the action page and tap “Install” - it will open Drafts and ask what action group to place the action. That’s it.

Is it possible to create a time stamp in the military time date group format (ddHHmm MMMyy or ddHHmm “Z” MMMyy)?

For example, 06/12/20 at 08:13 in the Central Time Zone is “060813SDec20”. Eastern time is “R”, Central is “S”, and so on. Frequently the time zone is omitted.

To insert a date/time in the format ddHHmm MMMyy, you can just use the date template tag with a format string like this.

The template tag and format used looks like this.

[[date|%d%H%M %b%y]]

The other format (ddHHmm “Z” MMMyy) is a bit tricker as it isn’t natively supported by strftime, but it can be achieved with a little bit of scripting. But for this reason, it does mean you can’t just drop it into a template for evaluation.

However, to provide an example as it may be useful elsewhere, here’s how I approached doing this.

It works quite similarly to the previous use of the date template tag, but it uses the JavaScript below to lookup the timezone offset identifier and then inserts the defined date/time string to the current selection and after that, jumps the cursor to the end of the newly inserted text.

let timezoneoffset = draft.processTemplate("[[date|%z]]")
let timezone = ""

switch (timezoneoffset)
{
	case "+0100":
		timezone = "A"
		break;
		
	case "+0200":
		timezone = "B"
		break;
		
	case "+0300":
		timezone = "C"
		break;
	
	case "+0400":
		timezone = "D"
		break;
		
	case "+0500":
		timezone = "E"
		break;
		
	case "+0600":
		timezone = "F"
		break;
		
	case "+0700":
		timezone = "G"
		break;
		
	case "+0800":
		timezone = "H"
		break;
		
	case "+0900":
		timezone = "I"
		break;
		
	case "+1000":
		timezone = "K"
		break;
		
	case "+1100":
		timezone = "L"
		break;
		
	case "+1200":
		timezone = "M"
		break;
		
	case "-0100":
		timezone = "N"
		break;
		
	case "-0200":
		timezone = "O"
		break;
		
	case "-0300":
		timezone = "P"
		break;
		
	case "-0400":
		timezone = "Q"
		break;
		
	case "-0500":
		timezone = "R"
		break;
		
	case "-0600":
		timezone = "S"
		break;
		
	case "-0700":
		timezone = "T"
		break;
		
	case "-0800":
		timezone = "U"
		break;
		
	case "-0900":
		timezone = "V"
		break;
		
	case "-1000":
		timezone = "W"
		break;
		
	case "-1100":
		timezone = "X"
		break;
		
	case "-1200":
		timezone = "Y"
		break;
		
	case "+0000":
		timezone = "Z"
		break;
}

editor.setSelectedText(draft.processTemplate('[[date|%d%H%M]]' + ' "' + timezone +  '" ' + '[[date|%b%y]]'))
let selRange = editor.getSelectedRange()
editor.setSelectedRange(selRange[0] + selRange[1], 0)

There seemed to be some variation in the use of spacing in the original post, so I’ve included them just in case. If not required, they can be easily removed.

Hopefully that all makes sense and helps for what you want.

1 Like

My apologies for the variation in spacing, I wasn’t sure of the best way to present the “Z” to make it more readable without the spaces.

I’m in the shallow end of the gene pool when it comes to programming, where does the code go with respect to the Template action “New draft with template”?

2 Likes

What is the date format if I want the date as ‘Wednesday, 10 April 2019’?

The template tag in the first example would just go directly in your template.

The second example you could run afterwards as a separate action, or you could integrate the script into an action by adding it into a script action step. That’s why I noted the second option couldn’t just be dropped in. If you have a look at the provided action above, you should see how it is included in that action. The key point here though is that this isn’t generic and presumably not something you would want to do with every template you use, so you would have to be “creative” in modifying Greg’s original action if you want to ensure it stays generic.

Given your statement above re. programming, it may simply be that, for now, using that first format is the way to go and with more experience you might come up with a good way for you to include in the original action; but in the interim you can always have the action I posted available to manually insert it if required.

1 Like

You can do this as follows.

[[date|%A %d %B %Y]]

Note the references to “strftime” above. If you Google that term, you’ll find lots of information about the available options.

Ok, I installed the script in a new action group. However I believe Daylight Savings Time may be playing havoc, the date I get when running it is: 100132 “R” Apr19

I live in the Central Time zone, which is “S”, which is offset -0600, it should now be -0500. I think I can fix that.

What I’d like it to look like is: 100132SApr19. How can I remove the quotation marks?

Thank you very much for your help with this. It will indeed get me started.

Got it! Here’s the result: 100207SApr19

I know, “whoopie”.

Thanks for all your help.

Try changing

editor.setSelectedText(draft.processTemplate('[[date|%d%H%M]]' + ' "' + timezone +  '" ' + '[[date|%b%y]]'))

to

editor.setSelectedText(draft.processTemplate('[[date|%d%H%M]]' + timezone +  '[[date|%b%y]]'))

The time zone is picked up from your device, so yes, you may need to account for that if you are in a DST observing location when working backwards to the identifier.

This is excellent.

One thing I would like is the ability to prompt the user for non-standard template items. So the code would use a service (probably a new one) to scan the selected template and use it to pop up prompts the user would fill in.

So, for example, a template might have [[customerName]] somewhere in it and the code would detect that and prompt me for “customerName”.

this is already existing: Share Parsed Template | Drafts Directory credits to @dansays

2 Likes

So, combining the two - the template chooser and this - would be handy.

I’ve not got time to try it this morning, but I’m wondering what would happen if you used double chevrons for the TextExpander snippet expansions and it’s fill in fields?

Unlike the action based option above, I’m curious if this would automatically trigger the capture and population of content via TextExpander?