What filter op behaves like "transclude field" ? (not "get")

[Edit: Solution found, see end of post]


I have a tiddler with an empty field like so:

title: very-long-title
bar: 

I also have a procedure to simplify the use of the very-long-title:

\procedure x() very-long-title

At a first glance the following filters should give the same output but…

[{very-long-title!!bar}!match[yes]then[correct]else[error]]correct

[<x>get[bar]!match[yes]then[correct]else[error]]error

The get does not accept an empty field.

How can the second filter be made to behave like the former and accept that bar-field is empty?

Thank you!


I think I got it! Use a function + its operator !

\function x-get(field) [very-long-title]get<field>

[function[x-get],[bar]!match[yes]then[correct]else[error]]

correct :grin:

Just a quick answer see the get and has operators, or use is blank, else and then operators to give a result on empty.

1 Like

Thanks Tones. While I did not investigate those options properly I’d think they’d reqire several filter runs in the filter expression which would make it long (I didn’t state that brevity was desired for the filters but it is somewhat important for legibility in this case).

You could also use the abbreviated form of the field:fieldname[fieldvalue] filter operator, like this:

\define x() very-long-title
{{{ [<x>!bar[yes]then[correct]else[error]] }}}

which works as desired in all these cases:

  • the bar field contains a value of “yes”
  • the bar field contains a value that is NOT “yes”
  • the bar field is empty
  • the bar field doesn’t exist
  • the very-long-title tiddler is missing

… and it’s even more concise than your previous function-based solution!

Note also that, while \procedure can be used to declare variables that contain simple text values, I prefer to use the old (but still valid) \define syntax for this purpose, since it makes it clear that this is just defining a convenient abbreviation for a longer text value.

see https://tiddlywiki.com/#field%20Operator. In particular:

Thus, !field:bar[yes] can be written as just !bar[yes]

enjoy,
-e

2 Likes

Hi @twMat, I think what @TW_Tones proposed is actually something like this

[<x>get[bar]else[]!match[yes]then[correct]else[error]]

i.e. if get doesn’t return anything because the field is empty, the else will add an empty item to the list which will not match yes and thus work as required. This is how I often do this kind of thing.

I do like @EricShulman’s solution better though, which I used to implement before forgetting about its existence (repeatedly). Also, since it returns the input tiddler instead of the field contents (if not empty), it is limited to such yes/no cases as yours, but requires an additional get to grab the actual contents of the field if you want to do something else with it.
Also, Eric probably meant to use the negated !bar[yes] to match your logic, or flip the correct and error logic (which would be a little easier to understand for me) in his example.
BTW, I also keep using \define for simple string assignments.

yeah… I missed the “!” in the OP example. I’ve edited my response to fix that.

-e

Oooh - that is soo superior to my solution! Thank you @EricShulman !!!

Also thank you @TW_Tones and @Yaisog :grin: