Converting all field values in currentTiddler to variables, how to? challenge!

79 views
Skip to first unread message

TonyM

unread,
Mar 30, 2020, 2:50:42 AM3/30/20
to TiddlyWiki
Hi Folks,

In the following macro I can simply generate fieldname=value pairs for all fields in the current tiddler, similar to how one would use the vars widget. It is handy because it only list the existing fields, however many they are. You do not need explicit set or Var statements.

  • We could use this to pass field names and values say in a new tiddler button, which I have done before.
  • You can also use it to pass variables to a modal

However I do not know how to use this or a similar method to convert these to actual variables for use in the current tiddler.

The issue is one needs to use either the set or vars parameter. 
  • The set required as many `</$set>` close statements as fields.
  • The vars needs only an open and close
  • However placing `<<field-value-pairs>>` inside a $vars widget - does not work
\define field-value-pairs()
\whitespace trim
<$list filter="[all[current]fields[]] -[[text]] -[[title]]" variable=fieldname>
{{{ [<fieldname>addsuffix[-var]] }}}=<$text text={{{ [all[current]get<fieldname>addprefix["""]addsuffix["""]] }}}/><br>
</$list>
\end
<<field-value-pairs>>

If you look inside $:/core/ui/ViewTemplate you can see how the variable storyTiddler is set, it then retains its value within the current story Tiddler. Because the loaging of each tiddler is wrapped in the vars statement.  I wonder if this is a place to put this? At least for tiddlers with a flag/tag/filed on them.

The alternative is define statements which also are not generated programmatically. I suppose with a trigger you could generate a tiddler full of defines and import that, but you will still need a trigger. We could use a trigger on "open tiddler" if one were available via the navigator mechanism.

Any Gurus up to this challenge?

Why?

Because I have come across a few cases where this would be useful, such as;
  • Creating a tiddler text template in a define where all values can use the same notation eg  $(varname)$
  • Rather than set such variable as needed, have them all available using a single method.
  • Avoid the need to complicate the wikitext with both {{!!fieldname}} and <<fieldname>> references inside and outside filters.
I look forward to an inventive solution (If I do not come up with one first)

Regards
Tony


Mark S.

unread,
Mar 30, 2020, 1:39:55 PM3/30/20
to TiddlyWiki

If you hack $:/core/ui/ViewTemplate/body to show

<$reveal tag="div" class="tc-tiddler-body" type="nomatch" stateTitle=<<folded-state>> text="hide" retain="yes" animate="yes">

<$list filter="[all[current]!has[plugin-type]!field:hide-body[yes]]">

<$tiddler tiddler=<
<currentTiddler>>>
<$transclude tiddler="transme">

<$transclude tiddler="$:/language/MissingTiddler/Hint"/>

</$transclude>
</$tiddler>

</$list>

</$reveal>

and create tiddler transme like:

\define pairs()
<$vars $(kp)$>
<$transclude field=text mode=block/>
</$vars>
\end
\define keypairs()
<$vars triple="'">
<$list filter="[all[current]fields[]] -[[text]] -[[title]] -created -modified -tags" variable=fieldname>
<<fieldname>>=<<triple>><$text text={{{ [all[current]get<fieldname>] }}} /
><<triple>>
</$list>
</
$vars>
\end
<$wikify name=kp text="""<<keypairs>>""" >
<$macrocall $name=pairs />
</$wikify>

Then this will do what you want, at least under some circumstances.

It may not work with all possible field content values.

The big problem was TW's overwhelming urge to "eat" all quote marks, leaving nothing to pass to the various macros.

Improvements should include:

* Only applying template for certain tagged tiddlers
* Create variable names like tony_<name> so that there is little possibility of conflict with existing variables in macros, environment

Be sure to put some sort of reminder in the file that you have hacked a core template so you don't get blind-sided on the next upgrade.

TonyM

unread,
Mar 30, 2020, 6:11:40 PM3/30/20
to TiddlyWiki
Mark

Nice work and thanks for this. The body seems the best place. I appreciate your effort.

Its good to see the techniques you have used.

I can handle the improvements you suggest.

To handle the core change I think a replacement viewtemplate may be wise, it can use all existing core tiddlers except a replacement body.

Regards
Tony

Reply all
Reply to author
Forward
0 new messages