Did you know ... | Search Documentation: |
Packs (add-ons) for SWI-Prolog |
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 |
No reviews. Create the first review!.
Version | SHA1 | #Downloads | URL |
---|---|---|---|
0.1.0 | 51090a8b31a72ffe346985f16dabeceaa4c3bb8d | 1 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.0/simple_web-0.1.0.zip |
0.1.3 | 3d68a216d3deb27d52f992beb853e242942dd6b7 | 3 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.3/simple_web-0.1.3.zip |
0.1.4 | feeaa4d0cb1dd85f25756151580e9c9d5f82cffd | 2 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.4/simple_web-0.1.4.zip |
0.2.0 | 06cda0edbe2a58acc947a30dfa152138034720b7 | 2 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.2.0/simple_web-0.2.0.zip |
0.2.1 | 1d374cfcb17ce8a90fbc283a422fcc19b7c9ea77 | 2 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.2.1/simple_web-0.2.1.zip |
0.3.0 | 6bd18048114530e20fe7feea058f51074a850b6e | 8 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.3.0/simple_web-0.3.0.zip |
0.3.1 | 69adc0c917f458053697562b5181db027535f7f0 | 26 | https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.3.1/simple_web-0.3.1.zip |
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).
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
A repository of examples, including using static, templates and creating an API can be found in the simple_web_examples repository.
route(Name, Path, Request)
where Request is a list of attr(val)
route(Name, Path, method(M), Request)
reply_template(Template, DataDict)
http:location(api, "/api", [])
, allows you to declare routes
with sw:route(all_data, api(all_data), method(get), _Request)
, where
api(all_data)
is equivalent to `/api/all_data`. See
http:location/3reply_404(Request)
. See
http_404/2reply_redirect(How, Where, Request)
. How is one of moved
,
moved_temporary
or see_other
. Where can be an atom ('/some/page'),
or an aliased path (root(some/page)
). See http_redirect/3
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 falseconfig(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).
Pack contains 9 files holding a total of 19.5K bytes.