As Charlie said, you could put each dropdown in a separate macro or procedure and keep all your select macros in the same tiddler. (You’ll probably also want to use a $select widget rather than HTML <select>
if you want selections to be stored somewhere in the wiki.)
title: Dropdowns
tags: $:/tags/Macro OR $:/tags/Global
\procedure dropdown1()
<$select field="field1">
<$list filter="[{myjson}jsonget[values1]]">
<option><<currentTiddler>></option>
<$list>
</$select>
\end
You’d then use <<dropdown1>>
in the tiddler where you want the dropdown to appear.
However, since each select
has so much code in common, I’d actually recommend using a single procedure (or macro) with parameters that can be specified when you call it.
\procedure my-dropdown(field, source:"myjson", index)
<$select field=<<field>>>
<$list filter="[<source>get[text]jsonget<index>]">
<option><<currentTiddler>></option>
<$list>
</$select>
\end
Use it like this:
<<my-dropdown field:"field1" source:"myjson" index:"values1">> <!-- each parameter specified by name -->
<<my-dropdown "field1" "myjson" "values1">> <!-- each parameter specified in the order they're given in the procedure definition -->
<<my-dropdown field:"field1" index:"values1">> <!-- "source" parameter omitted; it will default to "myjson", as specified in the definition. Note that due to the order in which I defined the parameters, I need to use index:"values"; if I omit the name of the parameter, "values1" will be treated as the value of <<source>> -->
<$transclude $variable="my-dropdown" field=<<fieldName>> index={{!!values}} /> <!-- using $transclude to call a macro/procedure lets you define the parameters using variables and/or transcluded values. The short form <<my-dropdown>> takes literal text strings only. -->
<$macrocall $name="my-dropdown" field=<<fieldName>> index={{!!values}} /> <!-- same as above; this was the preferred format before 5.3.0. -->
I used a procedure for the examples above, but macro syntax is very similar. I’d generally recommend using procedures over macros as they’re more robust and potentially more performant, but TW’s commitment to backwards compatibility means that, although macros (and $macrocall) are “deprecated”, they should continue to be supported in future versions of TW5.
For reference, here’s how I would write the my-dropdown
procedure in macro format:
\define my-dropdown(field, source:"myjson", index)
<$select field=<<__field__>>>
<$list filter="[<__source__>get[text]jsonget<__index__>]">
<option><<currentTiddler>></option>
<$list>
</$select>
\end
All the procedure-call options discussed above can also be used with a macro.