Commit 4447b27c authored by Paul's avatar Paul

Update to Simple Web v0.3.0

parent c921bc39
# Simple Web Examples
This repository contains example projects demonstrating how to use
Simple Web. Requires Simple Web version 0.2.0.
Simple Web. Requires Simple Web version 0.3.0.
1. [Hello World](examples/hello_world)
2. [Handling Static](examples/static_eg)
......
......@@ -7,14 +7,14 @@
:- location(api, "/api").
sw:route(root(.), _Request) :-
sw:route(home, root(.), _Request) :-
reply_html("<a href='/api/get_number'>Go To API</a>").
sw:route(api(get_number), method(get), _Request) :-
sw:route(get_number, api(get_number), method(get), _Request) :-
random_between(1, 10, Number),
reply_json_dict(resp{number: Number, status: success}).
sw:route(api(send_number), method(post), Request) :-
sw:route(send_number, api(send_number), method(post), Request) :-
read_json_dict(Request, Data),
NumStr = Data.get(number),
number_string(Number, NumStr),
......
......@@ -25,7 +25,7 @@ home_template(CustomDict, Template) :-
},
join_dicts(CustomDict, Home, Template).
sw:route('/', _Request) :-
sw:route(home, '/', _Request) :-
home_template(template{ title: 'Hello Bootstrap'
, content: content_eg
, content_message: "You can include data for templates too!"
......
......@@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">{{ end }}
{{ if title }}<title>{{= title }}</title>{{ end }}
{{ if styles }}{{ dynamic_include styles }}{{ else }}
<link href="/static/css/bootstrap.min.css" rel="stylesheet">{{ end }}
<link rel="icon" href="/static/images/favicon.ico">
<link href="{{= url_for("static('css/bootstrap.min.css')") }}" rel="stylesheet">{{ end }}
<link rel="icon" href="{{= url_for("static('images/favicon.ico')") }}">
</head>
<body {{ if body_attrs }}{{= body_attrs }}{{ end }}>
{{ if navbar }}{{ dynamic_include navbar }}{{ end }}
......@@ -16,9 +16,9 @@
</main>
{{ if scripts }}{{ dynamic_include scripts }}{{ else }}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="/static/js/vendor/jquery-slim.min.js"><\/script>')</script>
<script src="/static/js/vendor/popper.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script> {{ end }}
<script>window.jQuery || document.write('<script src="{{= url_for("static('js/vendor/jquery-slim.min.js')") }}"><\/script>')</script>
<script src="{{= url_for("static('js/vendor/popper.min.js')") }}"></script>
<script src="{{= url_for("static('js/bootstrap.min.js')") }}"></script> {{ end }}
</body>
</html>
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/main_styles.css" rel="stylesheet">
<link href="{{= url_for("static('css/bootstrap.min.css')") }}" rel="stylesheet">
<link href="{{= url_for("static('css/main_styles.css')") }}" rel="stylesheet">
......@@ -27,9 +27,9 @@ different but similar templates for all your related routes.
Using Simple Bootstrap, you can overcome the clobbering issue of
overwriting blocks by re-including them in your templates. For example,
with styles:
with styles using the provided `url_for` function:
```html
{{ dynamic_include bootstrap("styles") }}
<link href="/static/css/my_styles.css" rel="stylesheet">
<link href="{{= url_for("static('css/my_styles.css')") }}" rel="stylesheet">
```
......@@ -26,7 +26,7 @@ home_template(CustomDict, Template) :-
},
join_dicts(CustomDict, Home, Template).
sw:route('/', _Request) :-
sw:route(home, '/', _Request) :-
home_template(template{ title: 'Hello Bootstrap'
, content: content_eg
, content_message: "You can include data for templates too!"
......
{{ dynamic_include bootstrap("styles") }}
<link href="/static/css/main_styles.css" rel="stylesheet">
<link href="{{= url_for("static('css/main_styles.css')") }}" rel="stylesheet">
......@@ -5,7 +5,7 @@
:- use_module(library(sw/simple_web)).
sw:route('/', _Request) :-
sw:route(home, '/', _Request) :-
reply_html("<h1>Hello, world!</h1>").
serve :-
......
......@@ -8,14 +8,14 @@
% '/'
%
% Respond with image from static files
sw:route(root(.), _Request) :-
sw:route(home, root(.), _Request) :-
reply_html("<h1>Serving from static</h1>
<img src='/static/images/hedgehog.jpg' alt='hedgehog'/>").
% '/favicon.ico'
%
% Respond with image file from static files, cache for speed
sw:route(root('favicon.ico'), Request) :-
sw:route(favicon, root('favicon.ico'), Request) :-
reply_file('static/images/hedgehog.jpg', [cache(true)], Request).
serve :-
......
......@@ -23,3 +23,11 @@ config(template_dir, NewTemplateDir).
To save you from needing to pass options to `reply_template/3` all
the time, you can configure your own default options in the
`config.pl` file. These options are detailed in the Simple Web docs.
# URL_FOR
Some basic support for getting the URL for a route by name is supported
in the templates. You can see it demonstrated in `templates/post.html`,
where it returns the URL for home. Routes with variables aren't yet
supported with the exception of static URLs, this is demonstrated in
`templates/base.html` where it is used for the favicon.
......@@ -12,14 +12,14 @@
]).
sw:route('/', _Request) :-
sw:route(home, '/', _Request) :-
post_titles(Titles),
reply_template(blog, data{ titles: Titles
, title: 'Blog'
}
).
sw:route(root(Name), _Request) :-
sw:route(post, root(Name), _Request) :-
blog_post(Name, Content),
Data = data{ name: Name
, content: Content
......
......@@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" href="static/favicon.ico">
<link rel="icon" href="{{= url_for("static('favicon.ico')") }}">
<title>{{= title }}</title>
......
{{ block base }}
<h1>Blog Posts</h1>
<ul class="list-group">
{{ each titles, title}}<a href="/{{= title}}">
{{ each titles, title}}<a href="/{{= title }}") }}">
<li class="list-group-item">{{= title}}</li>
</a>{{ end }}
</ul>
......
......@@ -3,5 +3,5 @@
<p>{{= content}}</p>
<p><a href="/">←Back</a></p>
<p><a href='{{= url_for("home") }}'>←Back</a></p>
{{ end }}
......@@ -7,5 +7,5 @@
:- location(api, "/api").
sw:route(api(message), method(get), _Request) :-
sw:route(message, api(message), method(get), _Request) :-
reply_json_dict(resp{msg: "Hello world from Simple Web API!"}).
......@@ -29,5 +29,5 @@ view :-
% route '/' is only used in production, this will
% return the built index.html, which itself will
% request JavaScript and CSS from static.
sw:route(root(.), Request) :-
sw:route(home, root(.), Request) :-
reply_file('vueapp/dist/index.html', [cache(true)], Request).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment