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/*
Requires:simple_template

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.1.051090a8b31a72ffe346985f16dabeceaa4c3bb8d1https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.0/simple_web-0.1.0.zip
0.1.33d68a216d3deb27d52f992beb853e242942dd6b73https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.3/simple_web-0.1.3.zip
0.1.4feeaa4d0cb1dd85f25756151580e9c9d5f82cffd2https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.4/simple_web-0.1.4.zip
0.2.006cda0edbe2a58acc947a30dfa152138034720b72https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.2.0/simple_web-0.2.0.zip
0.2.11d374cfcb17ce8a90fbc283a422fcc19b7c9ea772https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.2.1/simple_web-0.2.1.zip
0.3.06bd18048114530e20fe7feea058f51074a850b6e8https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.3.0/simple_web-0.3.0.zip
0.3.169adc0c917f458053697562b5181db027535f7f026https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.3.1/simple_web-0.3.1.zip

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.

Setup

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

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1>{{= title }}</h1>
    {{ each items, item }}
        <h2>{{= item.title }}</h2>
        <div class="content">{{- item.content }}</div>
    {{ end }}
        <ul>
          <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>
        </ul>
  </body>
</html>

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.

Predicates

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.