Update the template language documentation to include the new 'with' statement

This commit is contained in:
Charles Haley 2025-09-15 13:20:35 +01:00
parent 9622ce41a8
commit dd880c8dea

View File

@ -279,6 +279,7 @@ General Program Mode
for_range ::= 'for' identifier 'in' range_expr ':' expression_list 'rof'
range_expr ::= 'range' '(' [ start_expr ',' ] stop_expr
[ ',' step_expr [ ',' limit_expr ] ] ')'
with_expr ::= 'with' top_expression ':' expression_list 'htiw'
list_expr ::= top_expression
break_expr ::= 'break'
continue_expr ::= 'continue'
@ -381,6 +382,29 @@ If the original Genre is `History.Military, Science Fiction.Alternate History, R
Note: the last line in the template, ``new_tags``, isn't strictly necessary in this case because ``for`` returns the value of the last top_expression in the expression list. The value of an assignment is the value of its expression, so the value of the ``for`` statement is what was assigned to ``new_tags``.
**with expressions**
The ``with`` expression:
#. changes the current book to the book with calibre book id (an integer) produced by valuating the ``top_expression``.
#. runs the ``expression_list``.
#. then resets the current book back to what it was.
The ``with`` expression returns the result of the last ``top_expression`` in the evaluated
``expression_list``, or the empty string if no expression list was evaluated.
For example, this template returns a list of the titles of each book selected in the GUI::
program:
res = '';
ids = selected_books();
for id in ids:
with id:
res = (if res then res & ', ' fi) & $title
htiw
rof;
res
**Return stmt**
Return the value of the ``expression``. If executed in a function then it returns the value of the expression to the caller. If executed in the outermost context (the template) then it sets the value of the template to the value of the expression and exits the template.