Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "simple_web"

Title:Microframework for building websites
Rating:Not rated. Create the first rating!
Latest version:0.3.1
SHA1 sum:69adc0c917f458053697562b5181db027535f7f0
Author:Paul Brown <paul@paulbrownmagic.com>
Home page:https://gitlab.com/PaulBrownMagic/simple_web
Download URL:https://gitlab.com/PaulBrownMagic/simple_web/*


No reviews. Create the first review!.

Details by download location


Simple-Web Framework

Easy, simple websites with Prolog.

Simply declare your routes as predicates and run the server. Simple-Web aims to help make web development simpler and easier. It does this by being opinionated. The scope of SWI-Prolog's capabilities are narrowed to a core subset, it requires a wise directory structure for static files, and it expects that you'll use simple_template for clever work with HTML.


Simple-Web depends on simple-templates, this can be installed via

?- pack_install(simple_template).

Example Basic Use

Create a directory with app.pl

:- use_module(sw/simple_web).

sw:route(home, '/', _Request) :-
    reply_html("<h1>Hello, world!</h1>
                <img src='static/images/logo.jpg' alt='logo'/>").

sw:route(templates_test, '/test', _Request) :-
    Data = data{ title: 'Hello'
               , items: [ item{ title: 'Item 1', content: 'Abc 1' }
                        , item{ title: 'Item 1', content: 'Abc 2' }
    reply_template(test, Data, options{cache:true}).

sw:route(termarized_test, root(termerized), _Request) :-
    reply_html([title('Termerized')], [h1('Termerized Example'), p('With some text')]).

sw:route(api, '/api', method(get), _Request) :-
    reply_json_dict(data{example: "Hello, world!"}).

:- run([port(5000)]).

Inside this root directory, create a folder called static to save your static files, such as your css, javascript and images. Place an image called logo.jpg into static/images/.

Also inside the root directory, create a folder called templates, inside this place test.html

    <h1>{{= title }}</h1>
    {{ each items, item }}
        <h2>{{= item.title }}</h2>
        <div class="content">{{- item.content }}</div>
    {{ end }}
          <li><a href="{{ url_for("templates_test")}}"</a></li>
          <li><a href="{{ url_for("termarized_test")}}"</a></li>
          <li><a href="{{ url_for("api")}}"</a></li>

Finally, run app.pl and navigate to http://localhost:5000

:~$ swipl app.pl

More Examples

A repository of examples, including using static, templates and creating an API can be found in the simple_web_examples repository.


Config Options

You can optionally create a config.pl file in the root directory of your application to change the default values:

For simple_web:

  • config(debug, true). Turn on http/http_error. Default false
  • config(static_dir, "/static"). Name of the directory in the web application root directory to serve static files from. Default "/static"
  • config(templates_dir, "/templates"). Name of the directory in the root directory in which templates are stored. *Default "/templates"*

    For simple_template, these are set to the same default's as simple_template and are documented by simple_template:

  • config(st_encoding, utf8).
  • config(st_extension, html).
  • config(st_cache, false).
  • config(st_strip, false).
  • config(st_frontend, simple).
  • config(st_undefined, error).

Contents of pack "simple_web"

Pack contains 9 files holding a total of 19.5K bytes.