2023-10-14 08:41:21 +00:00
created: 20230901122740573
modified: 20230901123102263
tags: WikiText
title: Conditional Shortcut Syntax
type: text/vnd.tiddlywiki
2023-12-04 08:53:24 +00:00
<<.from-version "5.3.2">> The conditional shortcut syntax provides a convenient way to express if-then-else logic within WikiText. It evaluates a [[filter expression|Filter Expression]] and considers the condition to be true if there is at least one result (regardless of the value of that result).
2023-10-14 08:41:21 +00:00
2023-11-28 11:51:56 +00:00
Within an "if" or "elseif" clause, the variable `condition` contains the value of the first result of evaluating the filter condition.
2023-10-14 08:41:21 +00:00
A simple example:
<$macrocall $name='wikitext-example-without-html'
2024-09-02 15:51:15 +00:00
src='<%if [{$:/info/url/protocol}match[file:]] %>
2023-10-14 08:41:21 +00:00
Loaded from a file URI
2024-09-02 15:51:15 +00:00
<%else%>
2023-10-14 08:41:21 +00:00
Not loaded from a file URI
2024-09-02 15:51:15 +00:00
<%endif%>
2023-10-14 08:41:21 +00:00
'/>
2024-09-02 15:51:15 +00:00
One or more `<%elseif%>` clauses may be included before the `<%else%>` clause:
2023-10-14 08:41:21 +00:00
<$macrocall $name='wikitext-example-without-html'
2024-09-02 15:51:15 +00:00
src='<%if [{$:/info/url/protocol}match[file:]] %>
2023-10-14 08:41:21 +00:00
Loaded from a file URI
2024-09-02 15:51:15 +00:00
<%elseif [{$:/info/url/protocol}match[https:]] %>
2023-10-14 08:41:21 +00:00
Loaded from an HTTPS URI
2024-09-02 15:51:15 +00:00
<%elseif [{$:/info/url/protocol}match[http:]] %>
2023-10-14 08:41:21 +00:00
Loaded from an HTTP URI
2024-09-02 15:51:15 +00:00
<%else%>
2023-10-14 08:41:21 +00:00
Loaded from an unknown protocol
2024-09-02 15:51:15 +00:00
<%endif%>
2023-10-14 08:41:21 +00:00
'/>
The conditional shortcut syntax can be nested:
<$macrocall $name='wikitext-example-without-html'
src='\procedure test(animal)
2024-09-02 15:51:15 +00:00
<%if [<animal>match[Elephant]] %>
2023-10-14 08:41:21 +00:00
It is an elephant
2024-09-02 15:51:15 +00:00
<%else%>
<%if [<animal>match[Giraffe]] %>
2023-10-14 08:41:21 +00:00
It is a giraffe
2024-09-02 15:51:15 +00:00
<%else%>
2023-10-14 08:41:21 +00:00
It is completely unknown
2024-09-02 15:51:15 +00:00
<%endif%>
<%endif%>
2023-10-14 08:41:21 +00:00
\end
<<test "Giraffe">>
<<test "Elephant">>
<<test "Antelope">>
'/>
Notes:
2024-09-02 15:51:15 +00:00
* Clauses are parsed in inline mode by default. Force block mode parsing by following the opening `<%if %>`, `<%elseif%>` or `<%else%>` with two line breaks
2023-10-14 08:41:21 +00:00
* Widgets and HTML elements must be within a single conditional clause; it is not possible to start an element in one conditional clause and end it in another
* The conditional shortcut syntax cannot contain pragmas such as procedure definitions