07-07-2013, 04:23 AM | #1 |
Enthusiast
Posts: 30
Karma: 10
Join Date: Apr 2013
Device: Kindle Touch
|
[SOLVED] Multiple conditions in TEMPLATE code
The TEMPLATE instructions show how to use various functions/commands to set values based on 1 condition. For instance; "Display Yes if the book has an EPUB format. Template:{formats:contains(EPUB, Yes,)}"
That works great. But, how do you code this if you want a "YES" where "formats" is one of a number of possible values; "EPUB" as well as "MOBI" and {however many more you wish} types? Thank you. Last edited by HFC3; 07-07-2013 at 01:39 PM. Reason: Annotate solved |
07-07-2013, 07:09 AM | #2 | |
Grand Sorcerer
Posts: 12,119
Karma: 73448614
Join Date: Nov 2007
Location: Toronto
Device: Nexus 7, Clara, Touch, Tolino EPOS
|
Quote:
When I am more awake I will post an example |
|
Advert | |
|
07-07-2013, 07:35 AM | #3 |
Grand Sorcerer
Posts: 12,119
Karma: 73448614
Join Date: Nov 2007
Location: Toronto
Device: Nexus 7, Clara, Touch, Tolino EPOS
|
Try
Code:
{:'test(list_intersection( 'MOBI,EPUB,PDF', approximate_formats(), ',') ,'Yes' ,'')'} |
07-07-2013, 07:39 AM | #4 |
Grand Sorcerer
Posts: 11,703
Karma: 6658935
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
First point: if your library has more than a thousand or so books, you should use the function that PeterT showed you in your original post: approximate_formats. The formats field verifies that the formats are actually on the disk, which can be slow. The approximate_formats function takes the formats from the database but does not verify them. In the vast majority of cases they produce the same answer, but they do not if formats "disappeared" through non-calibre manipulations.
To use approximate_formats you must use Template Program Mode. The template you posted above would become Code:
{:'contains(approximate_formats(), 'EPUB', 'Yes', '')'} Another solution uses the fact that the "pattern" argument to the contains function is a regular expression. Regular expressions can contain multiple subpatterns separated by the | character. Using that capability, the following template will display 'Yes' if approximate_formats contains 'EPUB' or 'MOBI'. You can add as many formats as you like. Code:
{:'contains(approximate_formats(), 'EPUB|MOBI', 'Yes', '')'} Code:
program: f = approximate_formats(); first_non_empty( contains(f, 'EPUB', 'Got an epub', ''), contains(f, 'PRC', 'Oh no, a prc', ''), contains(f, 'MOBI', 'Wow -- a mobi!', '') ) Last edited by chaley; 07-07-2013 at 09:22 AM. Reason: add link to calibre regexp tutorial |
07-07-2013, 01:39 PM | #5 |
Enthusiast
Posts: 30
Karma: 10
Join Date: Apr 2013
Device: Kindle Touch
|
I never though of putting the OR (|) inside the quotes. I was trying various OR's around the whole contains/approximate_formats conditions.
Thanks, all! |
Advert | |
|
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
help with saving template having multiple authors? | miquele | Library Management | 2 | 04-16-2013 04:49 PM |
BUG - Column Coloring, multiple conditions | icq70610 | Calibre | 8 | 12-26-2012 07:34 AM |
Request for generic 'multiple pages' code | fw190a8 | Recipes | 2 | 11-09-2011 09:22 AM |
Multiple Formats: Beatrice Beecham's Fete of Fate ($1.50) with enclosed Coupon Code | davejeffery | Self-Promotions by Authors and Publishers | 2 | 12-23-2010 03:08 AM |
Speaking to me:Terms and Conditions | Bob Russell | Lounge | 1 | 11-14-2005 09:01 AM |