![]() |
#1 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,970
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Template: field vs raw_field
I have an integer column with formatting {0}%
Which of these is better practice? Code:
program: if field('#percentread') == '0%' then '1' fi Code:
program: if raw_field('#percentread') ==# '0' then '1' fi Last edited by ownedbycats; 03-16-2021 at 01:41 AM. |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
It depends.
![]() The underlying problem is the same as you solve with check_yes_no(), what to do with undefined values. What do you want to happen in this case? Is 'undefined' the same as '0'? Is it something else? If defined, what comparisons are needed? For integers the field() function returns the empty string if the book's value is not defined, otherwise (as you said) the formatted value. If undefined is to be treated as zero and you aren't doing any non-equal comparisons then you need Code:
v = field('#percentread'); if !v || v == '0%' then
My preference is to use raw_field, setting the default to what makes sense in the context. That isolates me from the vagaries of formatting (which can change) and lets me use non-identical comparisons without thinking too much about it. If we assume that undefined values are to be treated as zero then this: Code:
v = raw_field('lookup key', 0) if v ==# 0 then And yes, I know I get rather pedantic. ![]() |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,970
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
On this topic:
Code:
program: if raw_field('#percentread') ==# '0' then '1' else raw_field('#percentread') fi I changed it to raw_field('#percentread', '1') and it remained undefined but that felt kludge. Empty string '' returned a 1 not sure why. Last edited by ownedbycats; 03-24-2021 at 02:31 AM. |
![]() |
![]() |
![]() |
#4 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
program: v = raw_field('#myint', ''); if !v then '' elif v ==# 0 then '1' else v fi Code:
program: v = raw_field('#myint'); if v == 'none' then '' elif v ==# 0 then '1' else v fi |
|
![]() |
![]() |
![]() |
#5 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,970
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
That works.
![]() |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
custom field based on multiple value field for export | nindotza | Calibre | 0 | 01-02-2021 08:37 AM |
Using built-in template functions in a custom template function | ilovejedd | Library Management | 4 | 01-28-2018 12:20 PM |
Copy data from Title field to IDs field | rlh3 | Library Management | 9 | 09-24-2012 08:12 AM |
Default value for read field (Yes/No-field) | opitzs | Library Management | 3 | 11-09-2011 07:17 AM |
Template error value:unknown field null | sfuller | Calibre | 4 | 06-13-2011 03:02 PM |