ITADN

[DOCS] Request to improve conditional documentation

#9848Openpmario 创建于 2026-05-01
actionable
P
pmariocommented
Request to improve conditional documentation Talk: https://talk.tiddlywiki.org/t/discussion-lets-introduce-a-select-statement/15251/8 I think @etardiff's example in reply to my initial confusion is a perfect example of what could be included in the documentation for conditional syntax. Her way of walking a reader through to her final suggestion is a perfect teaching methodology. From @etardiff: Here's what your conditional should look like if you strip out all the `{{{ }}}` from the `<%elseif ... %>` filters as @pmario said: ``` <%if [<f.TLSisempty GunType>match[yes]] %> {{!!State}} : {{!!CityTown}} - Unknown Gun Type <%elseif [<currentTiddler>get[GunType]has[nomenclature]] %> {{!!State}} : {{!!CityTown}} - {{{[<currentTiddler>get[GunType]get[nomenclature]]}}} <%elseif [<currentTiddler>get[GunType]has[common]] %> {{!!State}} : {{!!CityTown}} - {{{[<currentTiddler>get[GunType]get[common]]}}} <%elseif [<currentTiddler>get[GunType]has[abbreviated]] %> {{!!State}} : {{!!CityTown}} - {{{[<currentTiddler>get[GunType]get[abbreviated]]}}} <%else%> {{!!State}} : {{!!CityTown}} - Unknown Gun Type <%endif%> ``` However, one advantage of conditional syntax is that the *result* of each `%if`/`%elseif` filter is available under that filter as the variable `<<condition>>`. Furthermore, `get` returns only non-blank field values, so `get[nomenclature]` will automatically fail whenever `has[nomenclature]` is untrue. We can take advantage of these two facts to further simplify your code: ``` <%if [<f.TLSisempty GunType>match[yes]] %> {{!!State}} : {{!!CityTown}} - Unknown Gun Type <%elseif [<currentTiddler>get[GunType]get[nomenclature]] %> {{!!State}} : {{!!CityTown}} - <<condition>> <%elseif [<currentTiddler>get[GunType]get[common]] %> {{!!State}} : {{!!CityTown}} - <<condition>> <%elseif [<currentTiddler>get[GunType]get[abbreviated]] %> {{!!State}} : {{!!CityTown}} - <<condition>> <%else%> {{!!State}} : {{!!CityTown}} - Unknown Gun Type <%endif%> ``` And since this highlights some repetitive code, we could even take it a step further and store the "content" snippets as procedures, defined at the top of this tiddler: ``` \procedure unknown-type() {{!!State}} : {{!!CityTown}} - Unknown Gun Type \procedure known-type() {{!!State}} : {{!!CityTown}} - <<condition>> <%if [<f.TLSisempty GunType>match[yes]] %> <<unknown-type>> <%elseif [<currentTiddler>get[GunType]get[nomenclature]] %> <<known-type>> <%elseif [<currentTiddler>get[GunType]get[common]] %> <<known-type>> <%elseif [<currentTiddler>get[GunType]get[abbreviated]] %> <<known-type>> <%else%> <<unknown-type>> <%endif%> ``` Finally, you can use a $let widget to reduce some of the `<currentTiddler>get[GunType]` redundancy: ``` <$let gun-type={{{ [<currentTiddler>get[GunType]] }}}> <%if [<f.TLSisempty GunType>match[yes]] %> <<unknown-type>> <%elseif [<gun-type>get[nomenclature]] %> <<known-type>> <%elseif [<gun-type>get[common]] %> <<known-type>> <%elseif [<gun-type>get[abbreviated]] %> <<known-type>> <%else%> <<unknown-type>> <%endif%> </$let> ```
0 条评论