TW5 - Simple macro question: If fieldvalue is empty, true or false then render ...

232 views
Skip to first unread message

Måns

unread,
Apr 8, 2017, 7:15:24 AM4/8/17
to TiddlyWiki
Hi TwWizards

I am attempting to create a couple of global macros which will allow simple text templating.

My question is: How can a create a few simple macros which will render different results depending on a fieldvalue in a tiddler?:

Tagged: $:/tags/Macro
\define name() {{!!surname}}

\define hs() If the value of the field gender is true then show: "he". 
If gender is false then show: "she". If gender value is empty show: it
\end
\define Hs()  If the value of the field gender is true then show: "He". 
If gender is false then show: "She". If gender value is empty show: It
\end
\define hs'()  If the value of the field gender is true then show: "his". 
If gender is false then show: "her". If gender value is empty show: it's
\end
\define Hs'()  If the value of the field gender is true then show: "His". 
If gender is false then show: "Her". If gender value is empty show: It's
\end
\define gender()  If the value of the field gender is true then show: "boy". 
If gender is false then show: "girl". If gender value is empty show: boy/girl
\end

<<name>>

<$checkbox field="gender" checked="true" unchecked="false"><<gender>></$checkbox>

<<hs>>
<<Hs>>
<<hs'>>
<<Hs'>>

(As usual) I need some kind of "if-logic" ...

Cheers Måns Mårtensson

Thomas Elmiger

unread,
Apr 8, 2017, 8:13:51 AM4/8/17
to TiddlyWiki
Hi Måns

In my strex macro I use someting like this:

\define he-she-it(gendervalue:"_false_")

<$vars value="""$gendervalue$""">
<$set name="result" filter="[<value>!prefix[_false_]]" value=<<value>> emptyValue="it" >
<<result>>
</$set>
</$vars>
\end


Hope that works for you. (Not tested and composed on my mobile – watch out for typos)

Cheers,
Thomas

Thomas Elmiger

unread,
Apr 8, 2017, 8:20:36 AM4/8/17
to TiddlyWiki
Forgot to mention: my solution works, if you store "he" or "she" as values in the field "gender" and call my macro with this value as a parameter.

Of course this solves only one of your questions but maybe it helps as a starting point.

Måns

unread,
Apr 8, 2017, 9:14:18 AM4/8/17
to tiddl...@googlegroups.com
Hi Thomas

Thank you very much for answering :-)
If I do it like this:
\define he-she-it(gendervalue:"_false_") 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>!prefix[_
false_]]" value=<<value>> emptyValue="it" > 
<<result>> 
</$set> 
</$vars> 
\end 

<$checkbox field="gender" checked="he" unchecked="she"><<he-she-it {{!!gender}}>></$checkbox>

Nothing is shown when the field is either empty or doesn't exist.
It shows "he" when "he" is the value and "she" if "she" is the value.
It doesn't seem to do anything except rendering the fieldvalue when I use it like this..

Actually I don't need the neutral state - just: he, she, He, She, his, her. His Her computed from the state value (true or false) of a field (gender) (If the field does not exist a default value like: "gender not known" would suffice.

For convience I want to use short macros like <<g>>, <<G>>, <<g's>> and <<G's>> to render different results..

To me it seems like it should/would be some very simple (understandable) macros I should be able to string together with a few conditional "if's" here and there and wikitext... 
However I've been trying to do this in a TW for some years now - and I always have to give up because it gets to complicated for my (brain) usage :-) 
It always ends up with shooting sparrows with cannons eg: using Library tiddlers and what not ...

Thank you for trying to help me out - even if I don't understand the macro definition you posted :-)

Cheers Måns Mårtensson

Thomas Elmiger

unread,
Apr 8, 2017, 12:32:48 PM4/8/17
to TiddlyWiki
Hi again Måns,

Now this time we are going to solve thad riddle!

Try this and I try to explain gor your brain ;–)

---

\define he-she-it(gendervalue:"_false_")
<$vars value="""$gendervalue$""">
<$set name="result" filter="[<value>!prefix[_false_]]" value=<<out>> emptyValue="it">

result: <<result>>

</$set>
</$vars>

\end

<$checkbox field="gender" checked="he" unchecked="she"> male</$checkbox>

output: <$macrocall $name="he-she-it" gendervalue={{!!gender}}/>

---

It all starts with the macrocall instead of the shorter <<macroname param>> syntax. Frankly, I don’t know why.
If the input for the macro is empty, we assign the value "_false_" (maybe this works only with the macrocall syntax).
We assign the input value to a variable so we can use it in the filter later.
The result is the output of a filter: if the input value does not (!) equal "false" we assign the input value. Else the filter returns empty and we assign "it". (You can of course change that to "gender unknown" if you like.)

Finally we generate the output starting with "result: …"

Hope that helps. More to come …

Cheers,
Thomas

Thomas Elmiger

unread,
Apr 8, 2017, 12:37:38 PM4/8/17
to TiddlyWiki
Sorry, wrong code!

Try this:

\define he-she-it(gendervalue:"_false_")
<$vars value="""$gendervalue$""">

<$set name="result" filter="[<value>!prefix[_false_]]" value=<<value>> emptyValue="it">

Thomas Elmiger

unread,
Apr 8, 2017, 12:43:04 PM4/8/17
to TiddlyWiki
Hmm, correcting my own code I just realized that we don’t need the value=<<value>> part in the filter statement – this is the default.

Thomas Elmiger

unread,
Apr 8, 2017, 12:54:43 PM4/8/17
to TiddlyWiki
Part 2: Assigning other values by calling a macro via a variable.

Code:

---

\define He-She-check(gendervalue:"_false_")
<$vars value="""$gendervalue$""">
<$set name="result" filter="[<value>!prefix[_false_]]" value="He-She" emptyValue="no-value">

<$macrocall $name=<<result>> gendervalue=$gendervalue$ />

</$set>
</$vars>
\end

\define He-She(gendervalue)
<$vars value="""$gendervalue$""">
<$set name="result" filter="[<value>prefix[he]]" value="He" emptyValue="She">

result: <<result>>

</$set>
</$vars>
\end

\define no-value(gendervalue)

gender is not defined

\end

<$checkbox field="gender" checked="he" unchecked="she"> male</$checkbox>

output: <$macrocall $name="He-She-check" gendervalue={{!!gender}}/>

---

In the check-macro we test, if the value is not empty in the same way as before.
According to the test result we assign the name of the next macro we want to call.
If we have a value, we go to the He-She macro and check, if the value is "he". Then we generate a maching output "He" or "She".

O.k. for your brain?

All the best!
Thomas

Måns

unread,
Apr 8, 2017, 2:03:31 PM4/8/17
to TiddlyWiki
Hi Thomas

I'm afraid some kind of "neuronic" overloading is what is happening to my brain :-)

I tried this to get closer to what I want. I'm afraid it doesn't work this way:

\define He-She-check(gendervalue:"_false_") 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>!prefix[_false_]]" value="He-She" emptyValue="no-value">

<$macrocall $name=<<result>> gendervalue=$gendervalue$ />

</$set> 
</$vars>
\end

\define he-she(gendervalue) 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>prefix[he]]" value="he" emptyValue="she">

result: <<result>>

</$set> 
</$vars>
\end

\define He-She(gendervalue) 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>prefix[he]]" value="He" emptyValue="She">

result: <<result>>

</$set> 
</$vars>
\end

\define no-value(gendervalue)

gender is not defined

\end

\define him-her(gendervalue) 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>prefix[he]]" value="him" emptyValue="her">

result: <<result>>

</$set> 
</$vars>
\end

\define Him-Her(gendervalue) 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>prefix[he]]" value="Him" emptyValue="Her">

result: <<result>>

</$set> 
</$vars>
\end

\define his-her(gendervalue) 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>prefix[he]]" value="his" emptyValue="her">

result: <<result>>

</$set> 
</$vars>
\end

\define His-Her(gendervalue) 
<$vars value="""$gendervalue$"""> 
<$set name="result" filter="[<value>prefix[he]]" value="His" emptyValue="Her">

result: <<result>>

</$set> 
</$vars>
\end

\define g() <$macrocall $name="he-she-check" gendervalue={{!!gender}}/>
\define G() <$macrocall $name="He-She-check" gendervalue={{!!gender}}/>
\define g2() <$macrocall $name="him-her-check" gendervalue={{!!gender}}/>
\define G2() <$macrocall $name="Him-Her-check" gendervalue={{!!gender}}/>
\define g'() <$macrocall $name="his-her-check" gendervalue={{!!gender}}/>
\define G'() <$macrocall $name="His-Her-check" gendervalue={{!!gender}}/>

<$checkbox field="gender" checked="he" unchecked="she"> male</$checkbox>
output: <$macrocall $name="He-She-check" gendervalue={{!!gender}}/>

<<g>>
<<G>>
<<g2>>
<<G2>>
<<g'>>
<<G'>>

Thomas Elmiger

unread,
Apr 8, 2017, 2:57:40 PM4/8/17
to TiddlyWiki
Hi Måns

We are getting closer. Here’s some brain-candy for you:

What your gray cells missed was the fact that every variant would need a check-macro. Bit that was too complicated anyway.

Now I have generalized the check so it takes a second parameter called "nextmacro" to define the following macro. The check itself was therefore renamed to "gendercheck".

This worked for me.

Wish you a relaxing weekend!
Thomas

---

\define gendercheck(nextmacro:"He-She" gendervalue:"_false_")
<$vars next="""$nextmacro$""" value="""$gendervalue$""">
<$set name="result" filter="[<value>!prefix[_
false_]]" value=<<next>> emptyValue="no-value">
\define g() <$macrocall $name="gendercheck" nextmacro="he-she" gendervalue={{!!gender}}/>
\define G() <$macrocall $name="gendercheck" nextmacro="He-She" gendervalue={{!!gender}}/>
\define g2() <$macrocall $name="gendercheck" nextmacro="him-her" gendervalue={{!!gender}}/>
\define G2() <$macrocall $name="gendercheck" nextmacro="Him-Her" gendervalue={{!!gender}}/>
\define g'() <$macrocall $name="gendercheck" nextmacro="his-her" gendervalue={{!!gender}}/>
\define G'() <$macrocall $name="gendercheck" nextmacro="His-Her" gendervalue={{!!gender}}/>

<$checkbox field="gender" checked="he" unchecked="she"> male</$checkbox>
output: <$macrocall $name="He-She-check" gendervalue={{!!gender}}/>

g is <<g>>

G is <<G>>

g2 is <<g2>>

G2 is <<G2>>

g' is <<g'>>

G' is <<G'>>

Thomas Elmiger

unread,
Apr 8, 2017, 3:01:42 PM4/8/17
to TiddlyWiki
What I wanted to say: YOU were really close!

I am still working from my phone – that’s where all the typos come from.

Would be interesting to see your app once it is finished …

Thomas Elmiger

unread,
Apr 8, 2017, 4:41:28 PM4/8/17
to TiddlyWiki
Somehow copy-pasting corrupted our first filter for me – please make sure there is no linebreak in this:

filter="[<value>!prefix[_false_]]"

Måns

unread,
Apr 10, 2017, 3:29:12 PM4/10/17
to TiddlyWiki
Hi

For anyone who has followed this thread - or just found it and wants to see how the solution ended up looking like - here's a glimpse at the code:

\define if(value:"_false_",test,true,else,error) 
<$vars value="""$value$""" test="""$test$"""> 
<$set name="result" filter="[<value>!prefix[_false_]]" value="compare" emptyValue="if-error">
<$macrocall $name=<<result>> value=<<value>> test=<<test>> true="""$true$""" else="""$else$""" error="""$error$""" />
</$set> 
</$vars>
\end

\define compare(value,test,true,else,error) 
<$vars value="""$value$""" test="""$test$""" true="""$true$""" else="""$else$"""> 
<$set name="result" filter="[<value>prefix<test>]" value=<<true>> emptyValue=<<else>>>
<<result>>
</$set> 
</$vars>
\end

\define if-error(value,test,true,else,error:"undefined")
//$error$//
\end

\define male() <$macrocall $name="if" value={{!!gender}} test="he" true="male" else="female" error="choose gender"/>
\define g() <$macrocall $name="if" value={{!!gender}} test="he" true="he" else="she"/>
\define G() <$macrocall $name="if" value={{!!gender}} test="he" true="He" else="She"/>
\define g2() <$macrocall $name="if" value={{!!gender}} test="he" true="him" else="her"/>
\define G2() <$macrocall $name="if" value={{!!gender}} test="he" true="Him" else="Her"/>
\define g'() <$macrocall $name="if" value={{!!gender}} test="he" true="his" else="her"/>
\define G'() <$macrocall $name="if" value={{!!gender}} test="he" true="His" else="Her" error="gender not defined"/>

<$checkbox field="gender" checked="he" unchecked="she"> <<male>></$checkbox>

<$checkbox field="gender" checked=""> undefined </$checkbox>

!! Gender Test Results

g is ''<<g>>''

G is ''<<G>>''

g2 is ''<<g2>>''

G2 is ''<<G2>>''

g' is ''<<g'>>''

G' is ''<<G'>>''

I now have it working in my "teacher's tool for writing student statements" here: måns.dk/udtalelser/test

It's all in Danish I'm afraid. 


Happy hacking :-)

Cheers Måns Mårtensson
Reply all
Reply to author
Forward
0 new messages