- Documentation
- Reference manual
- Packages
- SWI-Prolog HTTP support
- The HTTP server libraries
- SWI-Prolog HTTP support
3.20.2 Repositioning HTML for CSS and javascript links
Modern HTML commonly uses CSS and Javascript. This requires <link> elements in the HTML <head> element or <script> elements in the <body>. Unfortunately this seriously harms re-using HTML DCG rules as components as each of these components may rely on their own style sheets or JavaScript code. We added a‘mailing' system to reposition and collect fragments of HTML. This is implemented by html_post//2, html_receive//1 and html_receive//2.
- [det]html_post(+Id,
:HTML)
//
- Reposition HTML to the receiving Id. The html_post//2
call processes HTML using html//1.
Embedded
\
-commands are executed by mailman/1 from print_html/1 or html_print_length/2. These commands are called in the calling context of the html_post//2 call.A typical usage scenario is to get required CSS links in the document head in a reusable fashion. First, we define css//1 as:
css(URL) --> html_post(css, link([ type('text/css'), rel('stylesheet'), href(URL) ])).
Next we insert the unique CSS links, in the pagehead using the following call to reply_html_page/2:
reply_html_page([ title(...), \html_receive(css) ], ...)
- [det]html_receive(+Id)
//
- Receive posted HTML tokens. Unique sequences of tokens posted with html_post//2
are inserted at the location where
html_receive//1 appears.
- See also
- - The local predicate sorted_html//1
handles the output of
html_receive//1.
- html_receive//2 allows for post-processing the posted material.
- [det]html_receive(+Id,
:Handler)
//
- This extended version of html_receive//1
causes Handler to be called to process all messages posted to
the channal at the time output is generated. Handler is
called as below, where
PostedTerms is a list of Module:Term created from calls to
html_post//2. Module is the
context module of html_post and Term is the unmodified term. Members in PostedTerms
are in the order posted and may contain duplicates.
phrase(Handler, PostedTerms, HtmlTerms, Rest)
Typically, Handler collects the posted terms, creating a term suitable for html//1 and finally calls html//1.
The library predefines the receiver channel head
at the
end of the
head
element for all pages that write the html head
through this library. The following code can be used anywhere inside an
HTML generating rule to demand a javascript in the header:
js_script(URL) --> html_post(head, script([ src(URL), type('text/javascript') ], [])).
This mechanism is also exploited to add XML namespace (xmlns
)
declarations to the (outer) html
element using xhml_ns//2:
- xhtml_ns(+Id,
+Value)
//
- Demand an xmlns:id=Value in the outer html tag. This uses the
html_post/2 mechanism to post to the
xmlns
channel. Rdfa (http://www.w3.org/2006/07/SWD/RDFa/syntax/), embedding RDF in (x)html provides a typical usage scenario where we want to publish the required namespaces in the header. We can define:rdf_ns(Id) --> { rdf_global_id(Id:'', Value) }, xhtml_ns(Id, Value).
After which we can use rdf_ns//1 as a normal rule in html//1 to publish namespaces from
library(semweb/rdf_db)
. Note that this macro only has effect if the dialect is set toxhtml
. Inhtml
mode it is silently ignored.The required
xmlns
receiver is installed by html_begin//1 using thehtml
tag and thus is present in any document that opens the outerhtml
environment through this library.