|
|
#1 |
|
hopeless n00b
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,127
Karma: 19597086
Join Date: Jan 2009
Location: in the middle of nowhere
Device: PW4, PW3, Libra H2O, iPad 10.5, iPad 11, iPad 12.9
|
Using built-in template functions in a custom template function
Help! It's been years since I've done any programming.
How do I use, for example, list_sort() inside a custom template function? Thanks! |
|
|
|
|
|
#2 | |
|
null operator (he/him)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22,034
Karma: 30277960
Join Date: Mar 2012
Location: Sydney Australia
Device: none
|
Quote:
Moderator Notice Writing calibre templates is not a Development issue, so I moved your thread to here. A search of this subforum, in particular this thread, should find some examples of list_sort usage. BR |
|
|
|
|
| Advert | |
|
|
|
|
#3 |
|
hopeless n00b
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,127
Karma: 19597086
Join Date: Jan 2009
Location: in the middle of nowhere
Device: PW4, PW3, Libra H2O, iPad 10.5, iPad 11, iPad 12.9
|
Sorry, I didn't explain better. I'm trying to create a custom function that would do a list_sort on lists inside a list.
The code below just gives me: S/R TEMPLATE ERROR global name 'list_sort' is not defined Code:
def evaluate(self, formatter, kwargs, mi, locals, val, start_index, end_index, sep):
if not val:
return ''
si = int(start_index)
ei = int(end_index)
# allow empty list items so counts are what the user expects
val = [v.strip() for v in val.split(sep)]
# sort John/Jane to Jane/John
for i, v in enumerate(val):
val[i] = list_sort(v,0,'/')
if sep == ',':
sep = ', '
try:
if ei == 0:
return sep.join(val[si:])
else:
return sep.join(val[si:ei])
except:
return ''
Last edited by ilovejedd; 01-28-2018 at 03:48 AM. |
|
|
|
|
|
#4 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24,905
Karma: 47303824
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
I'm pretty sure that in a template function like that you don't have access to other template functions. I think you will need to use Python functions to do this.
I think the following will work: Code:
val[i] = '/'.join(v.split('/').sort())
|
|
|
|
|
|
#5 |
|
hopeless n00b
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,127
Karma: 19597086
Join Date: Jan 2009
Location: in the middle of nowhere
Device: PW4, PW3, Libra H2O, iPad 10.5, iPad 11, iPad 12.9
|
Thanks.
The code above didn't quite work (something about not being iterable) but managed to get the function working with the ff: Code:
# sort John/Jane to Jane/John
separator = '/'
tmplist = []
for list1 in val:
res = [l.strip() for l in list1.split(separator)]
tmplist.append( separator.join(sorted(res)) )
val = tmplist
|
|
|
|
| Advert | |
|
|
![]() |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with template functions and library maintenance | Terisa de morgan | Library Management | 12 | 03-11-2017 10:43 PM |
| Problem with contains function in save template | MicaOlaAdams | Calibre | 5 | 10-21-2016 11:25 AM |
| how to use re() function in Template Program Mode? | msciwoj | Library Management | 3 | 07-07-2016 04:55 PM |
| template: if one of the tag is something... maybe contains or in_list functions | fxp33 | Calibre | 4 | 07-19-2014 06:18 AM |
| Making references to other fields in template functions | Ruskie_it | Library Management | 10 | 12-21-2011 11:53 PM |