Conditionally show part of a template

Is it possible to show a field conditionally in a template?

I have one for academic references: here is the ordinary one:

  • {{!!publisher}} ({{!!publisher-abbrev}}) //{{!!docname}}// ({{!!publisher-abbrev}}, {{!!year}})
  • Here is a version with a “hereafter”

  • {{!!publisher}} ({{!!publisher-abbrev}}) //{{!!docname}}// (hereafter referred to as {{!!hereafter}}) ({{!!publisher-abbrev}}, {{!!year}})
  • Not all my references will have a hereafter field set. Is it possible to only show the “hereafter” part of the template if it is present?

    Try this:

    {{!!publisher}} ({{!!publisher-abbrev}}) //{{!!docname}}//
    <$list filter="[<currentTiddler>has[hearafter]]">
       (hereafter referred to as {{!!hereafter}})
    </$list>
    ({{!!publisher-abbrev}}, {{!!year}})
    

    enjoy,
    -e

    1 Like

    An alternative syntax these days would be with the condional shortcut syntax

    {{!!publisher}} ({{!!publisher-abbrev}}) //{{!!docname}}//
    <%if [<currentTiddler>has[hearafter]] %>
       (hereafter referred to as {{!!hereafter}})
    <%endif%>
    ({{!!publisher-abbrev}}, {{!!year}})
    
    2 Likes

    I have a similar setup, however I’m wanting to expose multiple fields and display them in a single table. However in both those syntaxes (I’ve just switched from the <$list to the <%if version as it seems more logical for me to read) I get one table (with one line) per item. How would I join them into a single table, ensuring each line only shows if it exists and without relying on any other field existing (as would be the case if I nest the blocks)

    <%if [<currentTiddler>has[birthdate]] %>
    
    <!-- above line must be blank so the below line is interpreted as wikitext -->
    |! Birthdate| {{!!birthdate}} |
    <%endif%>
    <%if [<currentTiddler>has[deathdate]] %>
    
    |! Deathdate| {{!!deathdate}} |
    <%endif%>
    

    2025-07-24T12:27:48_deec4082

    Wikitext table formatting (using “|”) is somewhat limited in that it only works when formatting static tables where every specified row is unconditionally rendered. Thus, you could write:

    |! Birthdate| {{!!birthdate}} |
    |! Deathdate| {{!!deathdate}} |
    

    However, for your use-case (where you want to conditionally display a given row only when there actually is a value to show) this cannot be achieved using wikitext table formatting. To get the rendered results you want, you will need to fallback to using HTML table syntax instead. Something like this:

    <table>
    <%if [<currentTiddler>has[birthdate]] %>
       <tr><th>Birthdate</th><td>{{!!birthdate}}</td></tr>
    <%endif%>
    <%if [<currentTiddler>has[deathdate]] %>
       <tr><th>Deathdate</th><td>{{!!deathdate}}</td></tr>
    <%endif%>
    </table>
    

    -e

    1 Like

    Ah easy enough then. Thankyou!

    I use a lot of conditionally-rendered content, and I’ve found it convenient to define some procedures to make this sort of repetitive construction a little neater. For instance, to build on Eric’s answer, I might do something like this:

    \procedure row(field, label)
    \function header() [<label>!match[]] ~[<field>] +[sentencecase[]]
    <%if [<currentTiddler>has<field>] %>
    	<tr>
    		<th><<header>></th>
    		<td><$transclude $field=<<field>> /></td>
    	</tr>
    <%endif%>
    \end
    
    <table>
    <<row birthdate>>
    <<row deathdate "Died on">>
    </table>
    

    This doesn’t save you too much with only two rows, but gets more efficient the longer your table gets.

    6 Likes