Hello there! As the title says, I’m trying to understand how to look at the contents of a field, and if that field has integers in it, display it with the said integers formatted through a simple macro
(so they are displayed with an icon and a special color).
To give more context:
I’m working with a ViewTemplate that lists a number of fields. I have a main global macro like this:
\define entity(icon, label, value)
<div class="mark" title="$icon$ $value$"><span class="label entity clean"><span class="icon-$icon$">$label$</span></span><span class="data entity clean">$value$</span></div>
\end
which I’m using in other simpler macros like:
\define cost-hp(value)
<$macrocall $name=entity icon=cost-hp value=$value$/>
\end
So that when I use for example <<cost-fp {{!!base_cost}}>>, it displays the field “base_cost” (which contains “2” in the example below) like this:
(the “FP” is the icon here, all this is done through an icon font and CSS).
This works great when the fields contain exactly what I want displayed, no more, no less.
But staying in the current example, if that field contains for example “cannot be maintained”, rather than an actual cost of “2”, I’d like to NOT format it with the little icon and color. In other words, I’m looking for a simple method to format it if contains a number, and not if it doesn’t.
This is the complete View Template I’m working in:
<$list filter="[all[current]tag[GURPS Spell]]">
"""
//<$link to={{!!college}}>{{!!college}}</$link>// - ''{{!!class}}''<span style="float:right">//Source: {{!!source}}//</span>//<h3><$list filter="[<currentTiddler>has[prerequisites]]">''<hr/>Prerequisites'': {{!!prerequisites}}</$list></h3>//<hr/>
''Energy cost to cast'': <<cost-fp {{!!base_cost}}>><$list filter="[<currentTiddler>has[additional_cost]]">/ ''Additional cost'': {{!!additional_cost}}</$list>
''Time to Cast'': <<time {{!!casting_time}}>>
''Duration'': <<duration {{!!duration}}>><$reveal type="nomatch" state="!!concentration" text="No">, requires concentration to maintain.</$reveal>
''Items'': {{!!items}}
''Cost to create'': {{!!cost_item}}
"""
</$list>
The field I’m trying to format conditionally is “additional_cost”, so that if it doesn’t contain a number, it’s rendered normally ( {{!!additional_cost}}
), and, if it does contain a number, sort of reformat it so the number(s) and only the number(s) are formatted with the macro (<<cost-fp a_number>> to maintain
).
I was thinking this should be possible but may require the use of more advanced stuff than I’m capable to handle yet, like splitregexp and various clever syntaxes. Perphaps even copying the contains of the field to a temp tiddler or something… Anybody clever enough to figure it out elegantly?