| Did you know ... | Search Documentation: |
| Pack simple_web -- prolog/sw/simple_web.pl |
Easy, simple websites with Prolog.
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(template, '/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).
sw:route(termarized, 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('home') }}">Home</a></li>
<li><a href="{{= url_for('termarized') }}">Termarized Example</a></li>
<li><a href="{{= url_for('api') }}">API Example</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.
The following predicates are exported from this file while their implementation is defined in imported modules or non-module files loaded by this module.
The following predicates are exported, but not or incorrectly documented.