View Single Post
Old 02-23-2024, 04:41 PM   #4
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,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by calibricious View Post
I also must admit that, although I understand there are 3 different modes, I don't fully understand:
  • How to specify which mode I'm using (is it just syntax?)
Yes. TPM is differentiated from SFM by using quotes in specific places (see the manual). GPM templates start with the string "program:". Python templates (PTM) start with the string "python:"
Quote:
  • General Programming Mode. Honestly, I just didn't understand the grammar code block in the man page at all.
It is a fairly standard programming language syntax, with some stuff borrowed from Algol68 to disambiguate block structure. The grammar defines precedence and nesting.

The manual has lots of examples of GPM templates that should help. Look at those and use the calibre Template tester to experiment.
Quote:
  • What use cases are adequate for Template Program Mode and which ones are for General Programming Mode
GPM is best when the work to be done is complex. TPM is best if the work is simple and you need nesting, you want to use conditional text, or the template exists as part of a larger template expression. PTM is used when you need performance or want to use calibre's APIs.

Personally, I always use GPM or PTM to avoid syntax traps and task complexity outgrowing the other modes. Sometimes I use TPM that calls stored templates (Preferences / Advanced / Template functions) for the complex stuff and where I want to reuse the computation.
Quote:
  • When I can and can't use which of them (and whether I can mix them)
You can use any mode where you can use a template. You can't use GPM or python templates in template expressions, e.g.,
Code:
{tags} {#some_column:some_function()} {title:'maketitle($, $author, $series, $series_index, $#category, $#pairing)'}
There are many examples of templates in the manual, in MobileRead Template recipies thread, in Templates: various questions not worth their own thread, and under several of the spoilers the Template language changes thread's first post.
chaley is offline   Reply With Quote