Commit 4447b27c authored by Paul's avatar Paul

Update to Simple Web v0.3.0

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