View Single Post
Old 10-15-2020, 12:00 PM   #39
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,461
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by compurandom View Post
Ok, I just can't seem to get this to work in 5.2.
Before we get into the specific questions, it is worth talking a bit about the three template program modes: Single Function Mode (SFM), Template Program Mode (TPM), and General Program Mode (GPM).

SFM was intended to be 'simple', so it hides a lot of programming language bits. For example, the value of the column is always passed as the first argument to a function included in the template. It also doesn't support the difference between variables and strings; all values are strings.

Example, the following SFM template returns either the series name of the string "no series"
Code:
{series:ifempty(no series)}
The equivalent template in GPM is
Code:
program: ifempty(field('series'), 'no series')
Note that the first argument to ifempty is the value of the field 'series' and the second argument is the string 'no series'.

TPM is very close to GPM, but with oddities related to processing of {} characters and strings.

Personally, I use GPM unless the template is trivial, in which case I might use TPM. I never use SFM. In fact, if I could redo the template language development I would not build SFM.
Quote:
Originally Posted by compurandom View Post
I was hoping to find out what the value of connected_device_name() is for my devices, so I go into template tester and enter

Code:
{:connected_device_name()}
and it gives me empty for no device, but when I connect a device, I get

Code:
EXCEPTION:  connected_device_name: invalid storage location ""
There are two problems here. The first is that you are using SFM so there is an 'invisible' argument being passed to connected_device_name(), in this case the empty string because no column was specified. The second is that connected_device_name has 1 parameter, the storage location for the device. (NB: devices can have a different name per storage location.) In your SFM template the empty string is being passed as the storage location, and the function is telling you that location isn't valid.

Because of the invisible argument, connected_device_name must be used in TPM or GPM templates.

TPM:
Code:
{:'connected_device_name('main')'}
GPM:
Code:
program: connected_device_name('main')
Quote:
Originally Posted by compurandom View Post
You listed other functions, so I figured I'd try them. so I tried booksize() and it gives
Code:
EXCEPTION:  evaluate() takes 5 positional arguments but 7 were given
as do the others.
Here as well the invisible arguments are fouling things up. The booksize() function has no parameters. As above, you can use the function only in TPM and GPM.

TPM:
Code:
{:'booksize()'}
GPM:
Code:
program: booksize()
Quote:
Originally Posted by compurandom View Post
What am I missing?
In summary, two things:
  • Functions without parameters cannot be used in SFM.
  • The template tester has documentation for all the built-in functions. That is where you can see the function's parameters.

Quote:
Originally Posted by compurandom View Post
Oh, and thanks very very very much for the template in search... I just saved a search that finds books with the wrong number of tags, something I've wanted for a very long time.
You are welcome. I imagined that given their complexity, template searches would be saved and I ensured that worked. I also think that stored templates will be used (called) from template searches, making the search easier to read and debug, and facilitating searches with small variations.
chaley is offline   Reply With Quote