Quote:
Originally Posted by chaley
For the second template, for the bits that look like this:
Code:
if field('#li') then ' ✍️ ' & field('#li') else '' fi,
replace them with this call to a user-defined template function:
Code:
do_field('#li', ' ✍️ ')
To make this work you need to define the function. Put the following at the top of the template after the program: line.
Code:
def do_field(field_name, icon):
return (if v = field(field_name) then icon & v fi)
fed;
Reasons: - The long list of conditions becomes much easier to read.
- It saves editing if you want to change how an icon clause works.
- The 'if' in the function uses the same technique as above for performance improvement.
|
I liked this approach and now I have:
Code:
program: def do_field(field_name, icon, something, something2):
return (if v = field(field_name) then icon & v & something & something2 fi)
fed;
out = ' 💾 ';
fmts = list_sort(formats_sizes(), 0, ',') ;
for fmt_data in fmts:
fmt = sublist(fmt_data, 0, 1, ':');
size = sublist(fmt_data, 1, 2, ':');
str = if size ># 0 then strcat(fmt, ': ', human_readable(size), ' | ') else '' fi;
out=strcat(out, str)
rof;
if substr(out, -2, 0) == '| ' then
out = substr(out, 0, -3)
fi;
out
& strcat(
do_field('#li', ' ✍️ '),
do_field('#fav', ' ⭐:'),
do_field('#pp', ' 📈 ', ' pages') ,
do_field('#words', ' 📈 ', ' words') ,
do_field('#ac', ' 🟨 '),
do_field('#ot', ' 🌱 '),
do_field('#tr', ' 🌐'),
do_field('series', ' ▶️ ', ' - ' , field('series_index'))
)
It was cool to figure out how to concatenate the pages, words and series info, by adding those extra 'somethings', though I suspect there's a simpler way. I've added #ot (original title) and #tr (translator) as I have populated the db with this info for some titles.