Edit Toolbar: Apply Template

This code lets you apply a template when you edit a tiddler, or create a new one. The original idea belongs to @linonetwo and this code is inspired from his wonderful plugin Apply template to your new tiddler, A Template-List plugin: - Plugins - Talk TW

Also special thanks to @oeyoews for his great work on CodeMirror6 which supports applying templates. (see: [Release] Codemirror6 Plugin - #158 by oeyoews)

How it works

  • Create a new tiddler
  • Look at the Edit Toolbar
  • Click on the Apply Template button (the pen-plus icon)
  • Choose the desired template

Tutorial
msedge_img_386_20241206

Caution

  • When applying a template, the content of current tiddler including the text field will be overwritten.

Code

To give a try

How to add your own template
Here are steps to create a new template:

  1. Create a new tiddler, (name it e.g. $:/kookma/templates/source)
  2. Add meta fields and tags (these are used internally by code to work)
  • tag it with $:/tags/Template
  • add field tpl-caption and fill it with proper short name (like Source)
  • add field tpl-description and fill it with proper short description (like external reference)
  1. Add template fields and tags
  • Add all tags, your template should have
  • Add all fields with initial values, your template need to have
  • populate the body (text field) with heading, bullet items, initial notes, etc

As an example see below templates:

  • $:/kookma/templates/bookmark
  • $:/kookma/templates/credential
  • $:/kookma/templates/minutes

Credits

Ask for contribution

An area of improvement is to keep the current fields, tags, content. You may contribute and share your improved code.

2 Likes

Hello Mohammad,

Great idea.

Since the content of the tiddler is overwritten, wouldn’t it be more appropriate to see this feature as a create a new tiddler from a template and therefore to be a button in the page toolbar?

BR,

Eskha

Creating tiddler from a template exists in the core itself. For example, creating a Journal. Also in Mehregan Edition, you have shortcuts and also sidebar button to do so.

The idea here is to apply a template to a tiddler.

The issue of overwriting tiddler can be handled with extending the current codes. I like to have inputs from users to see how best handle this part,

This version keeps the contents (including all fields) and apply the template.

Updated

apply-templates-v3.json (5.7 KB)

1 Like

This is a great addition to TW! Thank you for implementing it.

1 Like

Thanks @Mohammad this is a useful addition, however I would urge people not to have large templates, because they result in textural bloat, that is repeating content that would be delivered once via a view template, only updated in one place after initial use, and consuming less total bytes.

1 Like

Certainly. But it does look as though this tool can be used to define (and prepopulate) other fields as well as, or perhaps instead of, the text field.

I find this technique to fall most of the way to the right on a scale of sophistication:

+---------------------------------------------------------------------------------------------------+
| New Tiddler ______________ Cloning and ______________ Using a _______X______ Using an Entry Form  |
|  every time                  Modifying               ViewTemplate    |       plus a ViewTemplate  |
|                                                                     /                             |
|                                                   This technique --'                              |
+---------------------------------------------------------------------------------------------------+
3 Likes

@TW_Tones
Even using viewtemplate, you have to create fields and tag different tiddlers with different set of tags! So, applying template is handy. Not all tiddlers have repeated text!

I should also note, in my workflow, I use important data in tags, title and text where TW can easily find them. Searching in fields always is harder than the standard way of searching in title, tags, text.

Hi @Mohammad I’d suggest using standard tag and field names, I can see you are also using $:/tags/Template, but why not use caption and description? The idea is from Suggestion for all Plugin developers about field name: Let’s use Ontology to maintain Interoperability

Thank you. I will read Suggestion for all Plugin developers about field name: Let's use Ontology to maintain Interoperability

@Mohammad I am not questioning the value, this is a useful tool, but with such power comes a responsibility, I am pointing out there are ways to use “content templating” that result in redundant and wasteful text, and there seems to be a tendency for new users to do this. It can also be manually intensive to reverse the consquences.

This is true but easy to address, and sometimes useful.

Hi @linonetwo

If caption and description fields were used as metadata for templates, they could not be at the same time used as values for newly created tiddlers based on these templates.
One solution for this problem could be to split templates metadata in dedicated “sidecart” tiddlers:

Template sidecart (template metadata):

caption: My fancy template
description: Use this as a tiddler prettyfier
tags: $:/tags/Template
target: myTemplate
title: myTemplate/sidecart

Template (template raw data):

caption: caption for new tiddlers
description: description for new tiddlers
tags: UserTag1 UserTag2
title: myTemplate

Here goes the template text...

Then in @Mohammad 's code the applied template title would be extracted from the sidecart target field.

Just an idea,

Fred

2 Likes

Here’s an illustration of the above concept:
apply-template-v3.1.json (6.1 KB)

I modified example templates and added sidecart template tiddlers in order to split data/metadata, and modified $:/kookma/ui/Buttons/apply-templates code to reflect these changes.

Fred

Hi @Mohammad,

I really like your idea, thanks for sharing it!

Now that you clone original tiddler before applying the template, do you think it could be useful to add a “revert to original” option to the “Apply Template” button?
If the temporary clone tiddler title used the qualify macro with a $:/temp/ prefix, then I believe clone title would be unique enough to allow content restoration.

Fred

I assume the current code do this. If you do not save the tiddler and click cancel operation, the original tiddler will be retrieved. But if you save the tiddler, the change will be permanent.

Oh, I didn’t thought about that. So let’s keep the tpl-caption.

And I find in my template-list plugin, I didn’t use these fields.

@Mohammad I also suggest using $:/tags/TextEditor/Template tag (I’m using this in my plugin, which simillar to $:/tags/TextEditor/Snippet in the core, I always check for core’s usage for potential compatability), means they are single tiddler template. This leave possibility for multi-tiddler template, like those paid Notion templates

Look at how Notion users gain money with 49$ each template. This is how TW could have achieved.

2 Likes

I used tpl-fieldname for meta data.
I would be happy to have a convention for naming meta data in TiddlyWiki.
I am thinking to:

meta-fieldname (all in lowercase)
or
mdf-fieldname (mdf: meta data field)
or
tw-fieldname

colon (:slight_smile: may be used instead of dash(-), but I assume dash or underscore is a better delimiter.

It is important to have consensus on this (create ontology for TW as you proposed).