![]() |
#1 |
Connoisseur
![]() 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?
|
![]() |
![]() |
![]() |
#2 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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, ) 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)); Code:
if "^Fallout$" inlist f then 'fallout.png:' fi You can also throw elses in there: Code:
if "^Fallout$" inlist f then 'fallout.png:' else 'null.png' fi 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. |
![]() |
![]() |
![]() |
#3 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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.
|
![]() |
![]() |
![]() |
#4 |
Connoisseur
![]() Posts: 91
Karma: 10
Join Date: Jul 2022
Location: Greenland
Device: xiaomi
|
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
|
![]() |
![]() |
![]() |
#5 | |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,971
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Quote:
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)); 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. Last edited by ownedbycats; 11-05-2022 at 01:45 AM. |
|
![]() |
![]() |
![]() |
#6 | |
Connoisseur
![]() Posts: 91
Karma: 10
Join Date: Jul 2022
Location: Greenland
Device: xiaomi
|
Quote:
|
|
![]() |
![]() |
![]() |
|
![]() |
||||
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 |