There are several problems with the template. First, in
Single Function Mode you cannot use template functions outside of a template item ({ ... }). Second, in Single Function Mode you should not use template items embedded in other template items ({foo {bar}}). Third, lookup doesn't take a search, instead taking metadata field names. Finally, given what you are trying to do, lookup isn't the best function to use.
Given the complexity of what you are trying to do, you should use
General Program Mode. That mode gives you much more control over function arguments, in particular permitting nested function calls. Also, I think that the str_in_list function is a better match than lookup.
This GPM template does what I think you want.
Code:
program:
str_in_list(
field('tags'),
',',
'Fiction',
template('Fiction/{title} - {authors}'),
template('Non-Fiction/{title} - {authors}'))
Just for completeness, the following GPM template is more efficient because it computes the title/author portion of the template one time and avoids using the rather slow function 'template'.
Code:
program:
ta = strcat(field('title'), ' - ', field('authors'));
str_in_list(
field('tags'),
',',
'Fiction',
strcat('Fiction/', ta),
strcat('Non-Fiction/', ta))
Finally, the "template tester" helps enormously when developing complex templates. It lets you edit a template and gives you immediate feedback on the resulting value or any errors. The tester is a "hidden" feature, not usually shown. I used Preferences / Toolbars to add the tester to the "Context menu for the books in the calibre library". That way the tester is available by right-clicking on a book. NB: pressing OK instead of Cancel makes the tester remember the last template so you can easily try it on another book