# Html\_write, DCG generators and backtracking

**URL:** <https://swi-prolog.discourse.group/t/html-write-dcg-generators-and-backtracking/3288>\
**Category:** General\
**Created:** [November 20, 2020, 9:48pm UTC](https://swi-prolog.discourse.group/t/html-write-dcg-generators-and-backtracking/3288 "2020-11-20T21:48:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [November 20, 2020, 9:48pm UTC](https://swi-prolog.discourse.group/t/html-write-dcg-generators-and-backtracking/3288/1 "2020-11-20T21:48:24Z")

</div>

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

```prolog
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?

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [November 21, 2020, 6:34am UTC](https://swi-prolog.discourse.group/t/html-write-dcg-generators-and-backtracking/3288/2 "2020-11-21T06:34:34Z")

</div>

There is foreach//2 and foreach//3 in [library(dcg/high\_order)](https://www.swi-prolog.org/pldoc/man?section=highorder). Here is a naive example with the two-argument version, without a separator:

```prolog
:- 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.

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

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

```

---

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [November 21, 2020, 7:39pm UTC](https://swi-prolog.discourse.group/t/html-write-dcg-generators-and-backtracking/3288/3 "2020-11-21T19:39:33Z")

</div>

Thanks, this is exactly what I was looking for!
