Thread: Custom Coloring
View Single Post
Old 06-04-2011, 09:57 AM   #64
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,463
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by jesscat View Post
Is it possible to construct more complex if-else statements in the template language? Like - if #unread and ondevice() then title red; elseif #unread and not ondevice() then green, else black? Would switch work - or does switch have to operate on a field?
First, you can do this with the wizard. Rules for a column are applied in order until one of them matches. In your case I see three rules, all for title. The first would test unread and ondevice, the second unread and not ondevice, and the third something like title is not empty (which is always true).

Now to the template, which is how I would do this. Use the 'and' function to combine the terms. If you think
Code:
if X and Y then Z else Q
you write
Code:
test(and(X, Y), Z, Q)
Use first_non_empty to sequence through a set of ands. For example,
Code:
if X and Y then Z1 elseif X and not Y then Z2 else Z3
becomes
Code:
first_non_empty(test(and(X, Y), Z, ''), test(and(X, not(Y)), Z2, ''), Z3)
A template that might work after you fix the column names is
Code:
program:
  first_non_empty(
    test(and(
      	        ondevice(),
                switch(field('#mybool'), 'Yes', '1', '')), 
        'red', ''),
    test(and(
                test(ondevice(), '', '1'),
                switch(field('#mybool'), 'Yes', '1', '')),
        'green', ''),
    'black')
Note that the not function is not working, which is why I used test in the second arm. I will fix the not function for the next release.
Quote:
One other thing: I did appreciate having the link to the tutorial on the template language; might there be any way to put that back in the "advanced" rule section? (Easier than having to go look for it when I find myself needing it...)
Good idea. I will do that.
chaley is offline   Reply With Quote