Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 11-03-2022, 12:51 AM   #1
Fynjisx
Connoisseur
Fynjisx began at the beginning.
 
Posts: 91
Karma: 10
Join Date: Jul 2022
Location: Greenland
Device: xiaomi
How to write templates?

Hello. I am a beginner and don't know many functions of caliber. I started studying templates and misunderstood a lot. Can you tell and apply examples of what templates help to do that cannot be achieved with ordinary means?
Fynjisx is offline   Reply With Quote
Old 11-03-2022, 02:10 AM   #2
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,971
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
First off, check out the template documentation. Bookmark it too.

I'll try to explain a few things with one of my own templates.

Here's part of a somewhat simple GPM template made to display icons for various fanfics rather than a zillion different column icon rules:

Code:
program:
	f = $#fanficcat;

   strcat(
		if "^Fallout$" inlist f then 'fallout.png:' fi,
		if "^Half-Life$" inlist f then 'halflife.png:' fi,
		if "^(Mass Effect Trilogy|Mass Effect: Andromeda)$" inlist f then 'masseffect.png:' fi,
		if "^The Elder Scrolls$" inlist f then 'theelderscrolls.png:' fi,
		if "^Pokémon$" inlist f then 'pokemon.png:' fi,		
		if "^Portal$" inlist f then 'portal.png:' fi,
   )
Defining things:

f = $#fanficcat;

Here, I am telling the template that 'f' means the value of the #fanficcat column. A column 'field reference' can be preceded with $ or $$; the former is input, while the latter is 'raw' input (the latter mostly matters for datetimes, which have a lot of different formattings).

While in the fanfic icon example I'm just using it as shorthand, here's another example from a more complicated template.

Here, I use regular expressions to extract two numbers (defined as 'f' and 's') from a column, do arithmetic on them, and then define the results of the math as 'newpercent.' This lets me use it further down the template.

Code:
f = re($#chapters, '(.*)/.*', '\1');
s = re($#chapters, '.*/(.*)', '\1');
newpercent = round(multiply ((f / s), 100));
GPM if-then templates:

Code:
if "^Fallout$" inlist f then 'fallout.png:' fi
This is an if-then template. If 'Fallout' is found in the list f, then output 'fallout.png:' and finish.

You can also throw elses in there:

Code:
if "^Fallout$" inlist f then 'fallout.png:' else 'null.png' fi
Here, if 'Fallout' is found, it'll output 'fallout.png'; otherwise, it'll output 'null.png:' and then finish.

Concatenation:

You notice that all the if-then templates are wrapped in a strcat() and that there's a comma at the end of each one.

This means that anything inside it will all be concatenated together. So if I have a book with "Fallout" and "Half-Life" in the column, instead of only displaying the first one, it'll output "fallout.png:halflife.png:" (and display both of them).

Last edited by ownedbycats; 11-03-2022 at 02:30 AM.
ownedbycats is offline   Reply With Quote
Old 11-03-2022, 06:00 AM   #3
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,445
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
There are hundreds of examples in the thread "Plugboard, template, and custom composite column recipes". I suggest you start with the last post and work backwards.
chaley is offline   Reply With Quote
Old 11-05-2022, 12:46 AM   #4
Fynjisx
Connoisseur
Fynjisx began at the beginning.
 
Posts: 91
Karma: 10
Join Date: Jul 2022
Location: Greenland
Device: xiaomi
Quote:
Originally Posted by ownedbycats View Post
Code:
f = re($#chapters, '(.*)/.*', '\1');
s = re($#chapters, '.*/(.*)', '\1');
newpercent = round(multiply ((f / s), 100));
.
explain how you made the number of read pages in books? How to make a column with the format i/pages, with the ability to extract i and pages separately? You will have to modify i manually every time, reading the pdf format
Fynjisx is offline   Reply With Quote
Old 11-05-2022, 01:16 AM   #5
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,971
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by Fynjisx View Post
explain how you made the number of read pages in books? How to make a column with the format i/pages, with the ability to extract i and pages separately? You will have to modify i manually every time, reading the pdf format
It isn't for pages. I use it to generate a new percent read after a fanfiction updates. It requires a very specific library/plugin setup that it wouldn't be helpful for most people.

However, I do have an Action Chain that'll let you input the current page number into a dialog, compare it against #pagecount, and then populate #percentread from it. I use it for physical books, or ones in PDF format.

Here's the template.

Code:
program:
	totalpage = $#pagecount;
	currentpage = globals(Current_Page=1);
	newpercent = round(multiply ((currentpage / totalpage), 100));
Note that globals(Current_Page=1) is the number inputted during the runtime of the action chain and it won't work outside the context of the chain (or at least it won't do anything useful).

However, if you want to play around with it: Open the Template Tester and replace the 'globals' bit with various numbers. (Don't forget the semicolon.) You'll see the value change depending on the #pagecount column of the selected books and whatever you define as currentpage.
Attached Files
File Type: zip UpdatePercentRead.zip (2.8 KB, 140 views)

Last edited by ownedbycats; 11-05-2022 at 01:45 AM.
ownedbycats is offline   Reply With Quote
Old 11-05-2022, 11:54 PM   #6
Fynjisx
Connoisseur
Fynjisx began at the beginning.
 
Posts: 91
Karma: 10
Join Date: Jul 2022
Location: Greenland
Device: xiaomi
Quote:
Originally Posted by ownedbycats View Post
Here's part of a somewhat simple GPM template made to display icons for various fanfics rather than a zillion different column icon rules:

Code:
program:
   strcat(
		if "^Fallout$" inlist f then 'fallout.png:' fi,
		if "^Half-Life$" inlist f then 'halflife.png:' fi,
		if "^(Mass Effect Trilogy|Mass Effect: Andromeda)$" inlist f then 'masseffect.png:' fi,
		if "^The Elder Scrolls$" inlist f then 'theelderscrolls.png:' fi,
		if "^Pokémon$" inlist f then 'pokemon.png:' fi,		
		if "^Portal$" inlist f then 'portal.png:' fi,
   )
but it can be done through the GUI interface!
Fynjisx is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
templates hobnail Sigil 19 09-13-2021 10:30 AM
Where to get Templates? chrismscotland Onyx Boox 1 07-25-2019 04:04 PM
Help using templates krakenmoth Library Management 2 04-10-2013 03:44 PM
templates? TAG_Keri Calibre 1 10-28-2011 02:11 PM
Write Fast, Write Slow? Moejoe Writers' Corner 14 03-25-2009 09:55 AM


All times are GMT -4. The time now is 09:32 AM.


MobileRead.com is a privately owned, operated and funded community.