How can I pass the value of the a field or text into a macro

Does anyone know I can pass the value of a field or text to a macro?
I have tried something like this:

<<MacroName "<$view field='FieldName'>" />>
or 
<<MacroName "{{!!FieldName}}" />>

In the macro tiddler, I used an alert to see what I would get:

exports.run = function(var1) {
        alert(var1);
        try {
            return  var1
        } catch (err) {
            console.error(err.stack)
            return "(ERROR: " + err.message + ") ";
        }
    };

I am not getting the output I want in the alert. I just want to get the text of value in that field.
Anyone know how I can achieve this?

Double-quotes are only for passing literal text.
To pass a field value, just use double curly braces, like this:

<<MacroName {{!!FieldName}}>>

Also, take note that because the double angle brackets surrounding the macro invocation don’t nest, when you want to pass a variable value, you need to use the $macrocall widget, like this:

<$macrocall $name="MacroName" var1=<<someVariable>> />

enjoy,
-e

1 Like
<<MacroName {{!!FieldName}}>>

I’m still not getting the field value. I am actually doing something a bit more complicated, and I guess I should have posted this before. But I am trying something like this:

<table>
    <$list filter="[<currentTiddler>fields[]prefix[cast-list]]" variable="cast">
        <tr>
          <td><$macrocall $name="MacroName" var1=<<cast>> /></td>
        </tr>
    </$list>
</table>

A field value should look something like this, where I am trying to split at the @ symbol:

cast-list 0: actor@role
cast-list 1: actor@role

In the macro tiddler I am trying something like:

(function() {

    /*jslint node: true, browser: true */
    /*global $tw: false */
    "use strict";

    exports.name = "MacroName";

    exports.params = [{
            name: "var1",
            default: ""
        },
        /*  {name: "var2"} - The default parameter is optional */
    ];

    /*
    Run the macro. Make sure it accepts the parameters you have defined above.
    */
    exports.run = function(var1) {
        var1 = var1.split('@');
        try {
            return var1[0] + "</td><td>" + var1[1];
        } catch (err) {
            console.error(err.stack)
            return "(ERROR: " + err.message + ") ";
        }
    };

})();

Unless I truly need JS, I stay in the realm of wikitext.

Is there some reason you can’t use https://tiddlywiki.com/#split%20Operator ?

I honestly tried to make that work, but I couldn’t figure it out.
What I have tried is something like this, where I am trying to replace the @ symbol with “TD” tags:

<table>
    <$list filter="[<currentTiddler>fields[]prefix[cast-list]split[@]+[</td><td>]]" variable="cast">
        <tr>
          <td><$macrocall $name="MacroName" var1=<<cast>> /></td>
        </tr>
    </$list>
</table>

Adding the “TD” tags in there is just a guess, and I even installed Tobibeer’s split plugin.

I’m not exactly following what you’re trying to achieve, but let’s see if this gets you closer…

<table>
    <$list filter="[<currentTiddler>fields[]prefix[cast-list]]" variable="cast">
        <tr>
          <td><$text text={{{ [<cast>split[@]first[]] }}}/></td>
          <td><$text text={{{ [<cast>split[@]last[]] }}}/></td>
        </tr>
    </$list>
</table>

Try this:

<table>
    <$list filter="[<currentTiddler>fields[]prefix[cast-list]]" variable="thisfield">
        <tr>
          <td><$text text={{{ [<currentTiddler>get<thisfield>split[@]nth[1]] }}}/></td>
          <td><$text text={{{ [<currentTiddler>get<thisfield>split[@]nth[2]] }}}/></td>
        </tr>
    </$list>
</table>

Notes:

  1. Don’t use Tobibeer’s split plugin. It was written before the TWCore had it’s own split[] filter operator, and is incompatible (specifically, if the split symbol doesn’t exist in the input, Tobibeer’s plugin returns an empty result, while the TWCore returns the entire input, unchanged). Using Tobibeer’s plugin will cause errors in the TWCore’s use of split[].
  2. The $list widget gets the fields that start with cast-list. Note: it’s a bad idea to use spaces within fieldnames. I suggest using something like cast-list-0, cast-list-1, etc.
  3. Each <td> takes the field value (with content of the form actor@role) and splits it at the @ symbol. The first <td> shows the actor text, the second <td> shows the role text.

Also, here’s an alternative that, instead of using separate fields for each cast/role entry, uses a single field to hold the entire cast list (e.g., “[[actor@role]] [[actor@role]] [actor@role]]”)

<table>
    <$list filter="[<currentTiddler>enlist{!!cast-list}]" variable="cast">
        <tr>
          <td><$text text={{{ [<cast>split[@]nth[1]] }}}/></td>
          <td><$text text={{{ [<cast>split[@]nth[2]] }}}/></td>
        </tr>
    </$list>
</table>

Notes:

  1. If an actor@cast value contains spaces it needs to be enclosed within doubled square brackets
    (e.g., [[John Smith@The Hero]] [[Jane Doe@The Damsel]]

…and, here’s a quick input form for constructing the list:

<$edit-text tiddler="$:/temp/actor" tag="input" default="" placeholder="enter actor's name"/>
<$edit-text tiddler="$:/temp/role"  tag="input" default="" placeholder="enter role"/>
<$button> add
   <$action-listops $field="cast-list" $subfilter="[{$:/temp/actor}addsuffix[@]addsuffix{$:/temp/role}]"/>
   <$action-deletetiddler $filter="[[$:/temp/actor]] [[$:/temp/role]]"/>
</$button>

Notes:

  1. The $action-listops joins the two inputs together (e.g. actor@role) and automatically adds doubled square bracket as needed, so you don’t have to type them.

enjoy,
-e

1 Like

In @CodaCoder’s example, the cast variable set by the $list widget will be the fieldnames that start with cast-list.

To produce the correct output, the <$text> widgets would then need to use filtered transclusions to get the actual fieldvalues like this:
{{{ [<currentTiddler>get<cast>split[@]first[]] }}}
and
{{{ [<currentTiddler>get<cast>split[@]last[]] }}}

-e

The solution is elsewhere in this thread thanks to eric, I just wanted to share a general rule, triggered by this example (which is now not relevant), but can provide a narrative way to consider tiddlywiki syntax.

Filters as with wiki text make use of various symbols to indicate different things. Such as {transcludethis} {!!currentfield} and <variable> . Like Einstein imagined sitting on a light beam, it can be helpful to imagine you are tiddlywiki trying to respond to your code.

When I do this I expect tiddlywiki to ask when seeing [</td><td>] are you referring to two variables inside an operand?

Thus to keep it simple it is better to “abstract” this so the filter remains clear;

\define td-to-td() </td><td>`
...
filter="... <td-to-td> ..."

That is any "strings that may confuse the syntax are saved in a macro/variable, and that macro variable is used instead.

The as a result tiddlywiki is much less likely to get confused and simply do as asked.

In a similar vein I think it important to consider that tiddlywiki already handles in wiki text html, such as <br> <hr> or even <details> so the idea of using macros and variables tiddlywiki “needs” double symbols <<macro>>to tell them apart from html, however since filters do not need to honor html then only one open and close symbol is needed eg; <td-to-td>. In the same rule in wiki text, but not filters, we need to use double symbols eg: {{tiddlername}} {{!!currentFieldname}} and with filtered transclusions we need another depth to set them apart {{{ filter }}}.

I hope this helps someone.