Html_write, DCG generators and backtracking

I’d like to use backtracking when generating html using http/html_write library to render views of all my solutions without shuffling around lists, something like

author(A), author_view(A)

which would emit whatever html comes out of author_view dcg rule for each author and concatenate these.

What would be the simplest way to achieve this?

There is foreach//2 and foreach//3 in library(dcg/high_order). Here is a naive example with the two-argument version, without a separator:

:- use_module(library(http/html_write)).
:- use_module(library(dcg/high_order)).

author('Stephen King').
author('Gore Vidal').
author('Marja Kekäläinen').

author_list --> html(ul(\authors)).

authors --> foreach(author(A), html(li(A))).

This converts the table into a list of items.

?- phrase(author_list, L), print_html(L).

<li>Stephen King</li>
<li>Gore Vidal</li>
<li>Marja Kekäläinen</li>
L = [nl(1), <, ul, >, nl(1), nl(1), <, li, >|...].

Thanks, this is exactly what I was looking for!

1 Like