3.20 The library(http/html_write) library
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog HTTP support
        • The HTTP server libraries
          • The library(http/html_write) library
            • html//1
            • page//2
            • page//1
            • html_begin//1
            • html_end//1
            • Emitting HTML documents
            • Repositioning HTML for CSS and javascript links
            • Adding rules for html//1
            • Generating layout
            • Examples for using the HTML write library
            • Remarks on the library(http/html_write) library
Availability::- use_module(swish(lib/html_output)).
Sourcehtml(:Spec)//
The DCG non-terminal html//1 is the main predicate of this library. It translates the specification for an HTML page into a list of atoms that can be written to a stream using print_html/[1,2]. The expansion rules of this predicate may be extended by defining the multifile DCG html_write:expand//1. Spec is either a single specification or a list of single specifications. Using nested lists is not allowed to avoid ambiguity caused by the atom []

  • Atomic data
    Atomic data is quoted using html_quoted//1.

  • Fmt - Args
    Fmt and Args are used as format-specification and argument list to format/3. The result is quoted and added to the output list.

  • \List
    Escape sequence to add atoms directly to the output list. This can be used to embed external HTML code or emit script output. List is a list of the following terms:

    • Fmt - Args
      Fmt and Args are used as format-specification and argument list to format/3. The result is added to the output list.
    • Atomic
      Atomic values are added directly to the output list.

  • \Term
    Invoke the non-terminal Term in the calling module. This is the common mechanism to realise abstraction and modularisation in generating HTML.

  • Module:Term
    Invoke the non-terminal <Module>:<Term>. This is similar to \Term but allows for invoking grammar rules in external packages.

  • &(Entity)
    Emit &<Entity>; or &#<Entity>; if Entity is an integer. SWI-Prolog atoms and strings are represented as Unicode. Explicit use of this construct is rarely needed because code-points that are not supported by the output encoding are automatically converted into character-entities.

  • Tag(Content)
    Emit HTML element Tag using Content and no attributes. Content is handed to html//1. See section 3.20.4 for details on the automatically generated layout.

  • Tag(Attributes, Content)
    Emit HTML element Tag using Attributes and Content. Attributes is either a single attribute of a list of attributes. Each attributes is of the format Name(Value) or Name=Value. Value is the atomic attribute value but allows for a limited functional notation:

    • A + B
      Concatenation of A and B
    • Format-Arguments
      Use format/3 and emit the result as quoted value.
    • encode(Atom)
      Use uri_encoded/3 to create a valid URL query component.
    • location_by_id(ID)
      HTTP location of the HTTP handler with given ID. See http_location_by_id/2.
    • #(ID)
      Abbreviated for for location_by_id(ID).
    • A + List
      List is handled as a URL‘search' component. The list members are terms of the format Name = Value or Name(Value). Values are encoded as in the encode option described above.
    • List
      Emit SGML multi-valued attributes (e.g., NAMES). Each value in list is separated by a space. This is particularly useful for setting multiple class attributes on an element. For example:
              ...
              span(class([c1,c2]), ...),

    The example below generates a URL that references the predicate set_lang/1 in the application with given parameters. The http_handler/3 declaration binds /setlang to the predicate set_lang/1 for which we provide a very simple implementation. The code between ... is part of an HTML page showing the english flag which, when pressed, calls set_lang(Request) where Request contains the search parameter lang = en. Note that the HTTP location (path) /setlang can be moved without affecting this code.

    :- http_handler('/setlang', set_lang, []).
    
    set_lang(Request) :-
            http_parameters(Request,
                            [ lang(Lang, [])
                            ]),
            http_session_retractall(lang(_)),
            http_session_assert(lang(Lang)),
            reply_html_page(title('Switched language'),
                            p(['Switch language to ', Lang])).
    
    
            ...
            html(a(href(location_by_id(set_lang) + [lang(en)]),
                   img(src('/www/images/flags/en.png')))),
            ...