Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 07-07-2013, 05:23 AM   #1
HFC3
Enthusiast
HFC3 began at the beginning.
 
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 02:39 PM. Reason: Annotate solved
HFC3 is offline   Reply With Quote
Old 07-07-2013, 08:09 AM   #2
PeterT
Taking a break; Fed up
PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.
 
PeterT's Avatar
 
Posts: 7,378
Karma: 46288753
Join Date: Nov 2007
Location: Toronto
Device: Wife: Touch, Arc, Vox Me: Nexus 7, Glo
Quote:
Originally Posted by HFC3 View Post
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.
By looking for a non empty intersection of your list of desired formats and the actual list of formats..

When I am more awake I will post an example
PeterT is offline   Reply With Quote
Old 07-07-2013, 08:35 AM   #3
PeterT
Taking a break; Fed up
PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.
 
PeterT's Avatar
 
Posts: 7,378
Karma: 46288753
Join Date: Nov 2007
Location: Toronto
Device: Wife: Touch, Arc, Vox Me: Nexus 7, Glo
Try

Code:
{:'test(list_intersection( 'MOBI,EPUB,PDF', approximate_formats(),  ',') ,'Yes' ,'')'}
PeterT is offline   Reply With Quote
Old 07-07-2013, 08:39 AM   #4
chaley
"chaley", not "charley"
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: 5,909
Karma: 1217216
Join Date: Jan 2010
Location: France
Device: Many android devices
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', '')'}
As for your question in this post, there are several ways to solve it. One will be the set manipulation that PeterT proposes, which I will leave to him.

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', '')'}
A more brute-force solution would be to use General Program Mode and test for each format explicitly. This would also allow you to customize the display for each format
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 10:22 AM. Reason: add link to calibre regexp tutorial
chaley is offline   Reply With Quote
Old 07-07-2013, 02:39 PM   #5
HFC3
Enthusiast
HFC3 began at the beginning.
 
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!
HFC3 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with saving template having multiple authors? miquele Library Management 2 04-16-2013 05:49 PM
BUG - Column Coloring, multiple conditions icq70610 Calibre 8 12-26-2012 08:34 AM
Request for generic 'multiple pages' code fw190a8 Recipes 2 11-09-2011 10: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 04:08 AM
Speaking to me:Terms and Conditions Bob Russell Lounge 1 11-14-2005 10:01 AM


All times are GMT -4. The time now is 01:12 AM.


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