Using fields as templates

After a short vacation, I’m continuing to work on my Periodic Table demo. I had thoughts for how to proceed discussed in Cascade-like mechanism with centralized logic. I never did get the approach @saqimtiaz suggested working properly. And while I may go back to it, a suggestion there from @TW_Tones struck me as simpler. Even so, I’m still stuck. :frowning:

The idea is that I have a number of data tiddlers and I want to be able to centralize the logic for how display their properties stored in a number of common fields.

I would love to have a config tiddler with something like this:

atomic-mass: A<sub>r</sub><sup>°</sup>({{!!symbol}}) = {{!!atomic-mass}}
atomic-number: {{!!atomic-number}}
atomic-radius: {{!!atomic-radius}} pm
electronegativity: Pauling scale: {{!!electronegativity}}
element-type: {{{ [{!!element-type}] }}}
...

(those transclusions might also start with the tiddler name, stored in a variable or parameter as needed.)

These I can do easily.

Then I would like to write a macro, say, displayfield, that accepts a field name parameter (and again, possibly a tiddler name) and then looks up in the config tiddler1 to find the matching template and applies this template to the current tiddler. I would use it simply as

<<displayfield "atomic-radius">  <!-- where currentTiddler is Copper -->  <!-- or -->
<<displayfield "atomic-radius" "Copper">

And expect back "1.6 pm". Note that the first row, atomic-mass is one of only two that uses anything from the data tiddler besides the field in questions. If that’s a problem, I can forgo doing so.

But I keep tripping on syntax for this. Is this fairly simple and I’m just missing it? Or is it more complex and I can stop beating myself about the head for not being able to figure it out? :wink:

Any hints from “go read this section” to “here’s a working example” would be appreciated.


1 Side question: does a macro have access to the tiddler fields in which it’s defined? It would feel cleaner, I think, for this config tiddler and the macro tiddler be the same thing. I can do it by using it by name, just as I would a separate config tiddler, but I just wondered if such access was already available.

I can share more later, Using currentTiddler having the macro call provide the fieldename (in a variable), then the macro first “gets the field-value” then each “display field format” refers to <<fieldname>> and <<field-value>> or in a filter as <fieldname> or <field-value> needed.

  • Personally I leave the label eg fieldname: out of the macro, so I choose if and how I want it labelled in the calling code.

I am still inclined to define a tiddler for each field $:/fields/fieldname because a “field tiddler” can store a lot more info than a field or data tiddler row. For example;

  • Different output formats
  • Field edit code
  • Aforementioned field-types
  • Value lookup filter or default values
  • and a number of other reasons.

I look forward to it. Thank you for your efforts.

Oh, absolutely. I just want to be able to get a display data back given the field name and value (and possibly the whole data tiddler for the two oddball cases.

I would expect <<displayfield "atomic-radius" "Copper"> to return 1.6 pm for tiddler Copper, which has an atomic-radius property of 1.6 and a configuration field (or tiddler or whatever) of something like, {{!!atomic-radius}} pm. I’ll edit the above, adding title and tags properties in an attempt to make it more clear that the list was supposed to be part of a tiddler

Yes, I will certainly consider it. It makes sense, but as I said before,

Even if I don’t start with that, I may end up with it for exactly the reasons you cite. But if I can’t do this basic part, all those extras will be meaningless. Basically, I know how to do this if the data is in the text field of a tiddler, but not when it’s elsewhere, like in another field.

Thank you very much for your help. I look forward to your further suggestions.

This philosophy of tiddlers has being challenged many times, and in my view never successfully dismissed.

  • A key facility to help is improving the handling of multiple tiddlers, including export/drag and drop.

Well; that is an excellent skills development opportunity and once you can, it is very empowering.

This is a great goal and there are different ways to handle it;

  • Personally I would make a package that the user can drag and drop or export that includes their modifications
    • I would be inclined to look at a plugin of source data + user modified tiddlers from the above + a few additional user interface or settings tiddlers.

:confused:

Thanks, I guess.

I have struggled with this, and will continue to do so, but I only posted because I’ve struggled a fair bit without success so far. I asked my other recent CSS question because I needed a break from struggling with this, and thought I’d start planning one of the next sections, and that demonstrated a gap in my knowledge. But I’d rather be working on this.

I can probably do most of this already. But I don’t know how to use a field as a template, and I haven’t found it in the documentation. Any hints would be welcome.

I’m not absolutely confident I understood your goals, but I just did a little experimenting and this seems to return your desired result when called with <<displayfield "atomic-radius">> (and <<currentTiddler>> = Copper):

There may be a more elegant or efficient way to do this than with the $wikify widget, but I hope this can get you started! I’m including the above tiddler in case it’s helpful.

config.json (345 Bytes)

1 Like

In its simplest form use a transclusion;

<$transclude tiddler=<<tiddlername>> field=<<fieldname>>/>
or
{{!!fieldname}}
or
{{tiddlername!!fieldname}}

If when you transclude it, it contains variables, macrocalls or subsequent transclusions these will be evaluated.

Here is a quick and simple demo

In a tiddler create a field created-relativedate with the following contewnt , Our display template
Created {{{ [all[current]get[created]format:relativedate[]] }}} /units
Now in the body of the tiddler transclude the field
{{!!created-relativedate}}

So the created-relativedate field contains the code to get and display in a given format give the current tiddler.

In the body of the tiddler we simply transclude the field, this pulls the content of field into the process when the body is rendered, where it then renders our triple curly braces “filtered transclusion”.

More complex solutions to operate on other tiddlers, within macros etc… are all just an extension of the same technique.

Another way to look at it;
Imagine I have a tiddler “formats”, containing a field relative-date with the following content; {{{ [<tiddlername>get<fieldname>format:relativedate[]] }}}

I could then use;

<$let tiddlername=<<currentTiddler>> fieldname=modified>
<$transclude tiddler=formats field=relative-date/>
</$let>

Yes, that’s it exactly. It’s the wikify widget I was missing. And the self-contained tiddler with the templates in the same place as the macro seems perfect for this use-case. I wonder, is there some deep reason for the roundabout use of a temporary variable as template or if it’s mostly an accident of history.

Thank you very much. I will work this into the next version

Keep in mind, a transclusion also typically results in it being wikified.

1 Like

Ahh, I had no idea. That will simplify everything. Thank you.

Tony’s right, and I was just in the process of editing my original post to reflect it, but I’ll leave it for posterity. This is all you really need:

1 Like