forked from erlyaws/yaws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.yaws
87 lines (58 loc) · 1.93 KB
/
query.yaws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<erl>
out(A) ->
{ssi, "TAB.inc", "%%",[{"query", "choosen"}]}.
</erl>
<div id="entry">
<h2>The query part of the url</h2>
<p>
A url can have an optional query part. This part is passed in
the A#arg.querydata which is passed as an argument to the
out/1 function.
</p>
<p>We show how to work with the query part of the url through
an example, if we have a URL on the form of
<a href="man.yaws?page=cat">http://yaws.hyber.org/man.yaws?page=cat</a>
a key/value pair is passed to the page.
In the above example, we have key=page and its value "cat".
The code in the page man.yaws, will read these key/value pairs
in the A#arg.querydata and display the man page.</p>
<p>
Assuming a predifined CSS class called box, defined as:</p>
<div class="box">
<pre>
div.box { border: solid; border-width: thin; width: 90%;
background: rgb(211, 211, 211) }
</pre>
</div>
<p>
The following code:</p>
<erl>
out(A) ->
{ehtml,ssi("man.yaws")}.
ssi(File) ->
{'div',[{class,"box"}],
{pre,[], {ssi, File, [],[]}}}.
</erl>
<p> will display a man page if invoked with a proper key/value
pair in the query part of the URL.</p>
<p> This fairly convenient way of getting at the query (or POST)
is equivalent of the code:</p>
<div class="box">
<pre>
P = yaws_api:parse_query(A),
L = case lists:keysearch(page, 1, P) of
{value, {page, Page}} ->
.....
</pre>
</div>
<p>The querypart of the URL is part as field in the Arg structure.
The function <tt>parse_query/1</tt> parses the raw data into
a key/value list. </p>
<p>The <tt>queryvar(ARG,Key)</tt> function returns the value of the
variable if it is found in the query part of the request. If the variable is not found
or if the variable is unset, the <tt>queryvar(ARG,Key)</tt> function returns <tt>undefined</tt>.
</p>
</div>
<erl>
out(A) -> {ssi, "END2",[],[]}.
</erl>