I have used a simple/atomic identifier in html_post//1
and html_receive//1
, both to make the example easier and to stay closer to the SWI-Prolog documentation. This identifier can also be a compound, but I did not get the use for that in your example.
The following code opens a Web browser with the generated HTML page. Standard HTML inspection tools show that the link
element is there now.
:- module(css_include, []).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(www_browser)).
:- http_server(http_dispatch, [port(5000)]).
:- http_handler(root(test), test, []).
test(_):-
reply_html_page(
[title('mytitle'),\html_receive(my_css_link)],
\html_body_stuff
),
www_open_url('http://localhost:5000/test').
html_body_stuff -->
{Url = 'http://www.swi-prolog.org'},
html_post(
my_css_link,
link([type('text/css'),rel('stylesheet'),href(Url)])
).
Edit: Based on a comment by @magus I have included the 'normal' way of generating an HTML page with a CSS link in the header, i.e. without using the html_post//1
and html_receive//1
. This may be useful for comparison with the other code snippet.
:- module(css_include, []).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(www_browser)).
:- http_server(http_dispatch, [port(5000)]).
:- http_handler(root(test), test, []).
test(_):-
reply_html_page(
[title('mytitle'),\a_css_link('http://www.swi-prolog.org')],
\html_body_stuff
).
a_css_link(Url) -->
html(link([type('text/css'),rel('stylesheet'),href(Url)])).
html_body_stuff -->
html(h1('HTML body over here.')).
The clause a_css_link//1
can be reused for other URIs as requested.
Hope this helps!