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, 04: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 01:39 PM. Reason: Annotate solved
HFC3 is offline   Reply With Quote
Old 07-07-2013, 07:09 AM   #2
PeterT
Grand Sorcerer
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: 12,119
Karma: 73448614
Join Date: Nov 2007
Location: Toronto
Device: Nexus 7, Clara, Touch, Tolino EPOS
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
Advert
Old 07-07-2013, 07:35 AM   #3
PeterT
Grand Sorcerer
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: 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' ,'')'}
PeterT is offline   Reply With Quote
Old 07-07-2013, 07:39 AM   #4
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: 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', '')'}
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 09:22 AM. Reason: add link to calibre regexp tutorial
chaley is offline   Reply With Quote
Old 07-07-2013, 01: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
Advert
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 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


All times are GMT -4. The time now is 09:33 PM.


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