How to determine if 2 fields are non-existent

I am trying to determine if a field exists, then if it doesn’t how to determine if the next field exists. Then if neither field exists call a macro.

{{{ [<currentTiddler>has:field[field-name-1]then{!!field-name-1}elseif?<currentTiddler>has:field[field-name-2]then{!!field-name 2}else<macro-name>] }}}

Is this even possible?

You were very close!

Use the :else filter run prefix, like this:

{{{ [<currentTiddler>has:field[field-name-1]then{!!field-name-1}] :else[<currentTiddler>has:field[field-name-2]then{!!field-name 2}] :else[<macro-name>] }}}

Note: To reduce typing, you can use ~ as a filter run prefix that is synonymous with :else.

Thus:

{{{ [<currentTiddler>has:field[field-name-1]then{!!field-name-1}] ~[<currentTiddler>has:field[field-name-2]then{!!field-name 2}] ~[<macro-name>] }}}

You can further reduce the syntax by combining the three filter runs together, like this:

{{{ [<currentTiddler>has:field[field-name-1]then{!!field-name-1}else<currentTiddler>has:field[field-name-2]then{!!field-name 2}else<macro-name>] }}}

Also, I notice that you are using has:field[...], which yields true if the field exists, even if the field value is empty. Depending on your particular use-case, if you can check for only non-blank field values, you can simplify the syntax by using the get[...] operator, like this:

{{{ [<currentTiddler>get[field-name-1]else<currentTiddler>get[field-name-2]else<macro-name>] }}}

enjoy,
-e

@EricShulman: I don’t believe this

[<currentTiddler>get[field-name-1]else<currentTiddler>get[field-name-2]

works.
The doc for else says

if the list of input titles is empty then return a list consisting of a single constant string, otherwise return the original titles

So, if field-name-1 exists, else will return its content and the subsequent get filter will fail (or yield unexpected results if field-name-1 contains a tiddler title).

Have a nice day
Yaisog

@Yaisog this is a worthwhile comment.

This can be made into a “valid syntax” but does not work.

TestTiddler.json (239 Bytes)

@TW_Tones: Good catch. Didn’t notice when copy-pasting that I forgot to add the closing bracket. But yeah, my comment was about @EricShulman’s else construction not being able to work as intended.

Have a nice day
Yaisog

2 Likes

You are right. If field-name-1 has contents, then the above filter will attempt to get[field-name-2] using the contents of field-name-1 as an input title. What I should have written is:

[<currentTiddler>get[field-name-1]] ~[<currentTiddler>get[field-name-2]]

which allows the else (~) conditional processing to apply to the entire second filter run. It’s essentially a problem of implied parentheses:

(A then B else A) then C
vs.
(A then B) else (A then C)

Good catch!
-e