How to check for empty parameter in macro

I know I am making some silly mistake here, but I just cannot figure it out :crazy_face:

This is my macro

\define title(t)

<$set name="hyphen" filter={{{ [<t>!is[blank]] }}} value="–" emptyValue="">

@@text-align: center;

! Some random title <<hyphen>> $t$

@@

</$set>
\end

What I want to do it is:

If user uses <<title "hello">> then it should print

Some random title – hello

If user uses <<title>> then it should print

Some random title

Note that there is no hyphen this time.

Problem 1

But this filter is not doing the job.

<$set name="hyphen" filter={{{ [<t>!is[blank]] }}} value="–" emptyValue="">

I have also tired

<$set name="hyphen" filter={{{ [[$t$]!is[blank]] }}} value="–" emptyValue="">

But I did not get the expected result

Problem 2

Why do I have to use $t$ syntax for the paramter?

I tried

Some random title <<hyphen>> <<t>>

It did not show anything. Isn’t t a variable like hyphen?

Try this :

\define title(t)

<$set name="hyphen" filter="[<__t__>is[blank]]" value="" emptyValue="-">

@@text-align: center;

! Some random title <<hyphen>> $t$

@@

</$set>
\end

<<title "Hello">>

<<title>>

https://demos.tiddlyhost.com/#How%20to%20check%20for%20empty%20parameter%20in%20macro

[<__t__>] in filters or <<__t__>> outside of filters means that you want to access the parameter of the macro as a variable.

$t$ means that you want a text substitution. By the way, here this work too : [[$t$]is[blank]] produce the same result than [<__t__>is[blank]]. The “parameter of the macro as a variable” notation is usefull when you are using a value with characters that can break a filter, like [ and ]. This is not an issue here because you probably wont ever write these characters inside your titles anyway.

<<t>> doesnt work because t is not a defined variable, here it’s a macro parameter.

You could also use a let widget to achieve this:


\define title(t)

<$let hyphen={{{ [<__t__>!is[blank]then[-]] }}}>

@@text-align: center;

! Some random title <<hyphen>> $t$

@@

</$let>
\end

<<title "Hello">>

<<title>>
1 Like

If I may restate your problem, staying away from code before I respond, because @telumire has answered your question based on the code you presented but presenting code is always a double edge sword because it can distract.

Problem restated
I want to pass a “title” into a string/or sentence and if present also display a hyphen ! Some random title [- $t$] [if title not blank]

Rephrasing this I see all you want is a conditional hyphen

\define title(title) 
@@text-align: center;
! Some random title {{{ [[$title$]!is[blank]then[-]] }}} $title$
@@
\end

<<title "Tony" >>
  • however to avoid a link you need <$text text={{{ [[$title$]!is[blank]then[-]] }}}/> to replace the hyphen.

Other notes;

  • Would you believe I never use the set widgets filter and value parameter at the same time? In fact the documentation implies it is incorrect to use both
    • value The value to assign to the variable if the filter attribute is missing or not empty
  • If it were not for the alignment we would not use a macro at all
  • Especially in examples to share be a little more generous in your parameter naming eg not “t” but “title”.

Even now I see my response was unnecessarily influenced by your code because we could do this (place “style” section in a stylesheet tiddler)

<style>
.centred {
   text-align: center;
}
</style>
<$let title="Tony">

!.centred Some random title <$text text={{{ [<title>!is[blank]addprefix[- ]] }}}/>

</$let>
  • Here I assume the title is available in a variable so set it with the let widget for this example.
  • I have long complained that we should not need to use such a verbose text widget in these cases but have a simple wikitext way to force it to text.

Lets digress;

Here is a work around for making the text widget hidden

Create a tiddler called text-only containing
<$text text=<<currentTiddler>>/>

Now introduce it in the filtered transclusion, rather than use the textwidget in the wiki text.
!.centred-title Some random title {{{ [<title>!is[blank]addprefix[- ]]||text-only }}}

So then I digress further, if we are transcluding lets go further;
Create a tiddler called hyphen-title containing
<$text text={{{ [<currentTiddler>!is[blank]addprefix[- ]]}}}/>

So back in our wiki text we can just use;
!.centred-title Some random title {{{ [<title>] ||hyphen-title }}}

  • notice how I convert <<title>> to a filter [<title>] but other forms are also valid [[$title$]]

As soon as you are going to do something more than once consider “modularising it” as I have with a transclude template, then generalise it so you can use it in even more cases;

  • You could in fact move everything into a transclusion if you needed to do it many times.

Happy tiddlywiki play, the font of eternal tinkering.

2 Likes

Thank you @TW_Tones and @telumire. These are great answer and have a lot of new information for me to digest.

I guess my confusion in picking the right syntax for the variable was the mistake.

@telumire you explained the variables syntax very well. I should make notes from it, so that I do not get confused again.

I had read this part of the documentation but I hadn’t paid attention to it.

So I settled on the following syntax

<$set name="hyphen" filter="[<__t__>!is[blank]then[-]]">

This filter evaluates to - or "".

One more question please

What is the difference between the following syntaxes?

<$set name="hyphen" filter={{{ [<__t__>!is[blank]then[-]] }}}>

and

<$set name="hyphen" filter="[<__t__>!is[blank]then[-]]">

Note that in one we have enclosed the filter in tripple braces {{{ }}} and in the second we have used quotes "".

Your answer blew my mind. I didn’t even consider using tranclusion for my problem.

Specifically this part

! Some random title {{{ [[$title$]!is[blank]then[-]] }}}

What is going on here? What is this feature? We are using filter without any widget?

This most likely just wrong; The triple braces causes the filter therein to be evaluated. So the argument to the filter= attribute becomes the ouput from that inner transclusion, which in this case will be either a - or (I think) nothing.

2 Likes

Well it works.

Example tiddler.json (315 Bytes)

Well, I have asked abovet that what is this feature. I don’t know the intricacies of this feature.

OK, so it is called “filtered transclusion”. The docs are very sparse but as I said, it basically evaluates the filter.

2 Likes

Yes the valid formats are
<$set name="hyphen" filter="[<__t__>!is[blank]then[-]]">
OR
<$set name="hyphen" value={{{ [<__t__>!is[blank]then[-]] }}}>

The following is a miss use

  • <$set name="hyphen" filter={{{ [<__t__>!is[blank]then[-]] }}}>
    but will work because it becomes
  • <$set name="hyphen" filter="-"> which becomes a tiddler titled “-” or <$set name=“hyphen” filter="">`nothing.

You would be familiar with transclusions such as {{tiddlername}} or {{!!fieldname}}, these are shorthand forms of the $transclude widget. the filtered transclusion or triple braces is just another shortcut form {{{ filter }}} which is “like using a filter in a $list widget”. The filtered transclusion is replaced with the result of the filter. The key uses are;

  • Quickly see the outcome of a filter
  • Use to provide the value to an attribute eg attribute={{{filter}}} when the attribute is for css, html, widgets or in a $macrocall.
  • Use to calculate a value given to variable, as in the second $set widget above.
  • Use to retrieve a value easily described in a filter especially using variables eg {{{ [all[current]get<fieldname>] }}} which lets us get the fieldname from a variable and then get that field names value.

Filtered transclusions - triple curly braces are

  • Quick and easy to use
  • Can simplify the amount of code to achieve things
  • Can specify a template
  • Became particularly useful when we got the mathematics operators eg
    • {{{ [{!!cost}multiply{!!qty}multiply[1.1]] }}} Cost x Qty + 10%

See Transclusion in WikiText not much info as @twMat says. See also a mention here Introduction to Lists . Keep an eye out and you will see there uses.

  • Perhaps we can add more documentation.

[Edited]
Another use I suggested before now was string assembly or concatenation, the best use case is the button or other widgets tooltip parameter eg;
tooltip={{{ [[Add tag]] [<currentTag>] to this tiddler [<currentTiddler>] +[{!!description}] [join[]]

4 Likes

Thank you so much @TW_Tones for the detailed write up. It will probably take me a life time to learn all the features and nuances of TW.

Yes especialy as change is always happaning, but its fun.