Creating tiddlers and programmatically modifying them?

140 views
Skip to first unread message

TonyM

unread,
Mar 23, 2020, 9:20:52 PM3/23/20
to TiddlyWiki
Folks,

I have a little project where it would be helpful if I could create a tiddler from a template (for its body) but then replace various content within the text or fields with different values.

For example what if the template included ##name## then I had a macro that would replace in the body of the new tiddler
  • ##name## with Tony
  • ##icon## with {{<<tiddlername>>||$:/core/images/info-button}}
  • etc...
I imagine if a set of such changes could be triggered by a button and an action widget all the better.

I have other ways to achieve this but it would surprise me if the code was not already available somewhere.

Any leads would be appreciated

Regards
Tony

Flibbles

unread,
Mar 23, 2020, 10:19:05 PM3/23/20
to tiddl...@googlegroups.com
This would be outside the scope of Relink, but  there are ways to programmatically fill out a generated tiddler.

\define body()
Dear $(name)$,

I write to inform you that your pet
, $(pet)$ has filed for emancipation.
\end

\define createButton()
<$button tooltip="Create new letter" aria-label="new letter" class=<<tv-config-toolbar-class>>>
<$action-sendmessage $message="tm-new-tiddler" title=<<title>> text=<<body>> />
<$list filter="[<tv-config-toolbar-icons>prefix[yes]]">
{{$:/
core/images/new-button}}
</$list>
<$list filter="[<tv-config-toolbar-text>prefix[yes]]">
<span class="tc-btn-text"><$text text="new letter" /
></span>
</$list>
</
$button>
\end


<$set name="title" value="New Letter">
<$set name="name" value="Wilson">
<$set name="pet" value="snuffles">
<<createButton>>

</$set></$set></$set>


This will create a new tiddler. with a variable title, name, and pet.

-Flibbles

TonyM

unread,
Mar 24, 2020, 1:08:40 AM3/24/20
to TiddlyWiki
Flibbles,

Thanks, I never intended to be part of relink, only you are working on code to replace things within the text field according to what surrounds it.
  • So I was thinking perhaps you could extract the code to say replace ##placeholder## with "this".
  • Just a suggestion ignore at will
But then your suggestion is a good one

\define body()
Dear $(name)$,

I write to inform you that your pet
, $(pet)$ has filed for emancipation.
\end
I tested it you can place the above in a tiddler  tagged $:/tags/Macro and it works just as well.

There may be a variation on this trick, so I expect I will find something even more useful from this lead.

Thanks for the great Tip

Tony

PMario

unread,
Mar 24, 2020, 5:18:29 AM3/24/20
to TiddlyWiki
Hi,
IMO see the GH pull request: https://github.com/Jermolene/TiddlyWiki5/pull/4436  You could promote it.
It seems I don't have much success with my PRs lately.
-mario

TonyM

unread,
Mar 24, 2020, 6:07:29 AM3/24/20
to TiddlyWiki
Mario

I already did support that Mario. It would help.

I have found that unless I can automate building things like new novel buttons or build templates it consumes too much time.

If you look at the core buttons as an example they are not strait forward.

The tip of using a macro for the body and $(variable)$ references is a good one. I have a macro that converts all fields on a nominated tiddler to variables which would work well with this. One reason its good is all values are represented with the same standard, no need to choose between variable, field or tiddler references

I am also keen to enable users, non tiddlywiki enthusiasts to build forms or templates to generate output like one may do with a mail merge so finding an easy template language that I feed with data from tiddlers would be nice. Like someone writes a letter and inserts active variables.

Regards
Tony

TonyM

unread,
Mar 24, 2020, 8:35:19 AM3/24/20
to TiddlyWiki
Fibbles,

I have just realized this won't work in the tiddler I create needs to contain a define
\define code-tiddler() $(name)$

There are many applications it will, but perhaps not this one.

Regards
Tony


On Tuesday, March 24, 2020 at 1:19:05 PM UTC+11, Flibbles wrote:

Flibbles

unread,
Mar 24, 2020, 8:49:11 AM3/24/20
to TiddlyWiki
Why doesn't it work?

I'm unsure whether you want $(name)$ to be replaced during creation, or if you want to copy `\define code-tiddler() $(name)$` exactly as is, but either way, you can include pragma inside the body macro.

\define body()
\define code-tiddler() $(value)$

Dear $(name)$,

I write to inform you that your pet
, $(pet)$ has filed for emancipation.
\end


$(value)$ here will remain as-is unless `value` is defined. If it IS defined, it'll replace, so you can get either way you want it, I'd think.

TonyM

unread,
Mar 24, 2020, 6:20:48 PM3/24/20
to TiddlyWiki
Flbibles,

I did not comprehend "nested" pragma, but thank you for pointing out it works.

because I did not realise this I started looking at other methods, however I can return to your suggestion now.

best wishes
Tony

Mohammad

unread,
Mar 25, 2020, 12:45:34 AM3/25/20
to TiddlyWiki
Flibbles,

This is really amazing!
I did not know Tiddlywiki allows nested macros! This can open another window in scripting in TW

--Mohammad
Reply all
Reply to author
Forward
0 new messages