View Single Post
Old 01-24-2025, 05:10 PM   #783
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,465
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Comfy.n View Post
I'm finding it fun to assemble these templates but I'd be happy to receive some feedback on overall syntax and alternatives to simplify the code or perhaps convert it to PTM
Two suggestions that will make the templates easier to read, at least for me.

For the first template, replace bits that look like
Code:
if field('pubdate') then 'Published in ' & field('pubdate') else '' fi
with this
Code:
if v = field('pubdate') then 'Published in ' & v  fi
Reasons:
  • Putting the assignment in the if condition avoids another call to field(), improving performance.
  • The else clause isn't needed because the if returns the empty string if the condition is false.

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.
chaley is offline   Reply With Quote