This is a stripped-down version of a single section of Grok TiddlyWiki, optimized for fast loading and readability by search engines. Some features are missing.

For the full Grok TiddlyWiki experience, please visit the wiki version of this page.

Ex:NowTimestampFunction/answer

 12th October 2024 at 5:40pm

The first step to figuring this out is probably to try it some other way and see what happens. So let's do that:

\procedure now-timestamp() <<now [UTC]YYYY0MM0DD0hh0mm0ssXXX>>

<<now-timestamp>>

20241019235625068

Hmm, no, that looks just fine. So what about the hint in the exercise? Let's try setting a field on the current tiddler to the time. You might use this to click a button to indicate that you've reviewed a tiddler at a particular time, say.

\procedure now-timestamp-proc() <<now [UTC]YYYY0MM0DD0hh0mm0ssXXX>>
\function now-timestamp-func() [<now [UTC]YYYY0MM0DD0hh0mm0ssXXX>]

<$button set="!!nowtime" setTo=<<now-timestamp-proc>> >
  Set Reviewed Time (Badly)
</$button>
<$button set="!!nowtime" setTo=<<now-timestamp-func>> >
  Set Reviewed Time (Properly)
</$button>

* Time Now: {{!!nowtime}}
* Field Value: <$view field="nowtime"/>

  • Time Now: 20241019235625069
  • Field Value: <<now [UTC]YYYY0MM0DD0hh0mm0ssXXX>>

Aha! Here the function works as you would expect, but the procedure simply puts the literal text of the procedure body in the field. (This is hard to notice if you just transclude the value, because the macro call to now expands when you do so. The $view widget doesn't wikify the field, so it shows the true value, as does editing the tiddler and looking at its field values.)

The million-dollar question is, of course, why?

Recall that we are transcluding the value of now-timestamp as an attribute of the $button widget, namely setTo. This means that the value will only be wikified once! Consequently, the literal text of the procedure appears in the output. Changing it to a macro instead of a procedure doesn't help, because there's no way to use text substitution to call the now macro.

But when we change it to a function, the effect of the <<>> syntax changes! It will still only be wikified once, but wikifying a function call results in evaluating the filter expression within the function – and evaluating this filter expression requires calling the now macro, so that happens during the single wikification.

You may find this trick a useful alternative to verbose uses of the $wikify widget on occasion.

Go to question: Ex:NowTimestampFunction