Getting The Content of Custom Widget in a Variable

How can I access the content of a custom widget in the form of a variable?

Example:

\widget $wdg.first(a: "test")
here I want to process the content of widget!
\end $wdg.first

and calling it like:

<$wdg.first a=120>
Comma Separated Values
Name, Age, Weight
Fariba, 23, 68
Ula, 37, 72
Shima, 19, 56
</$wdg.first>

I want to get access to the content of $wdg.first and for example create a table from CSV data that is given as the widget content.
I know, I can pass this as attribute value, but here I want to use the widget content (body).

Unfortunately, at the moment you can’t. See this topic for details.

Most likely, the closest alternative at the moment would be to have a parameter for your widget for the CSV, and then when calling the widget, you can put the CSV in an attribute using triple quotes """:

\widget $wdg.first(a: "test", csv)
Here you can access with the notation `<<csv>>`
\end $wdg.first

<$wdg.first a=120 csv="""
Comma Separated Values
Name, Age, Weight
Fariba, 23, 68
Ula, 37, 72
Shima, 19, 56
""" />

Thank you for your reply and pointing me to the previous discussion.
It is a pity one cannot access the content! This way a procedure seems easier than a custom widget!

The only way to access the result of a widget, before you display something is to use the wikify widget which is discouraged.

  • When I say display something, I am talking about the final render to html step that TiddlyWiki does.

As general rule now I say to myself “a widget, custom or otherwise is where information goes to be displayed”. Think of widgets as being a container which may even contain other widgets, even buttons and other interactions but that each widget is a container and is not available as a variable, it generates output.

  • Consider a list or button widget, they produce a result, they display a list or a button.
  • Inside the list you can access a variable such as the current tiddler, but outside you can’t access the list widget as a variable (without wikification).
    • Since list widgets iterate filters, keep this as filter, or as a function or a procedure that need not be wikified.

This points one to use functions or variables (let set vars) and only a subset of procedures and macros, that do not need wikification.

  • In your csv data example it is literals, so placed inside a procedure, it can be made use of without wikification.
  • The best thing about functions, or triple curly braces and the new back tick attributes is they are evaluated “in place” so their wikification is unnecessary.
    • That I mean here by evaluation is to retrieve variables, fields, and tiddler content and evaluate any filter operators, to produce an output.
    • functions are a way of expressing a filter as a variable, it returns the first value, to return more than one value, join[] them together, and if necessary enlist them later.

To use the content of a widget call in a widget definition, you need to use the $slot="ts-raw" but this is a widget and has the above limitations. I asked for this to be made available as a variable.

Finaly, if you are trying to generate a complex output you want to use as input in another place, the transclusion process can also be used.

  • For example your above csv data could be in its own tiddler and transcluded, rather than a widget.
  • I still need to work on a detailed position on transclusions as a form of wikification.
    • In someways when you transclude you are saying wikify everything within that transclusion NOW, and return that result. Transclusions can be given as an attreibute to a macro or widget.

Thank you @TW_Tones. It is surprising that the widget content is not accessible. I read your previous discussion with Jeremy!
The $wikify here is not efficient and I try to use it in very special cases when it is really needed.
Here I decided to go with a procedure.