Here’s another use for my favorite new widget from the 5.2.5 release, $genesis
, that will make your source code look better (subjectively):
Imagine you want to display the results of a filter in an <ul>
, so you might do something like
<$list filter="[<filter-sequence>first[]]" variable="void">
<ul>
<$list filter="[<filter-sequence>]">
<li>
<<currentTiddler>>
</li>
</$list>
</ul>
</$list>
There are probably other ways to do this, but let’s assume that’s what you’d do.
Note that <filter-sequence>
is a placeholder for a sequence of filter steps.
Now if <filter-sequence>
is rather computation-intensive (like full-text searches and such in a large wiki), you’d compute it only once and save the results in a variable like this
<$set name="results" filter="[<filter-sequence>]">
<$list filter="[<results>!is[blank]]" variable="void">
<ul>
<$list filter="[enlist<results>]">
<li>
<<currentTiddler>>
</li>
</$list>
</ul>
</$list>
</$set>
Now, with $genesis
, this can be made prettier:
<$set name="results" filter="[<filter-sequence>]">
<$genesis $type={{{ [<results>!is[blank]then[ul]] }}}>
<$list filter="[enlist<results>]">
<li>
<<currentTiddler>>
</li>
</$list>
</$genesis>
</$set>
I don’t think it will be faster, as the filter expressions are the same, but it avoids the nested $list
s which personally I always find a bit cluttered.
Note that this only works with the TW ≥ 5.2.6-prerelease fixed version of $genesis
.
PS: This also works with <table>
instead of <ul>
and <tr>
instead of <li>
.