File: examples.en.html

package info (click to toggle)
rivet 0.5.0-3.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,676 kB
  • ctags: 1,345
  • sloc: sh: 8,394; xml: 6,622; ansic: 4,367; tcl: 2,813; makefile: 101; lisp: 78; sql: 25
file content (174 lines) | stat: -rw-r--r-- 9,429 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Examples and Usage</title><link rel="stylesheet" href="rivet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.66.1"><link rel="start" href="index.en.html" title="Apache Rivet"><link rel="up" href="index.en.html" title="Apache Rivet"><link rel="prev" href="commands.en.html#id4764506" title="unescape_string"><link rel="next" href="tcl_packages.en.html" title="Rivet Tcl Packages"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Examples and Usage</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="commands.en.html#id4764506"><img src="images/prev.png" alt="Prev"></a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="tcl_packages.en.html"><img src="images/next.png" alt="Next"></a></td></tr></table></div><div class="section" lang="en"><div class="titlepage"><div><div><hr><h2 class="title" style="clear: both"><a name="examples"></a>Examples and Usage</h2></div></div></div><p style="width:90%">
      Some examples of Rivet usage follow.  Some prior Tcl knowledge
      is assumed.  If you don't know much Tcl, don't worry, it's easy,
      and there are some good resources available on the web that will
      get you up to speed quickly.  Go to the <a href="help.en.html#websites" title="Web Sites">web sites</a> section and have a look.
    </p><div class="example"><a name="hello world"></a><p class="title"><b>Example1.Hello World</b></p><p style="width:90%">
	As with any tool, it's always nice to see something work, so
	let's create a small "Hello World" page.</p><p style="width:90%">
	Assuming you have Apache configured correctly, create a file
	called <tt class="filename">hello.rvt</tt> where Apache can find
	it, with the following content:
      </p><pre style="background:#bbffbb ; width:90ex ; margin: 2ex ;      padding: 1ex; border: solid black 1px ; white-space: pre;      font-family:monospace ; " class="programlisting">&lt;?
puts "Hello World"
?&gt;
</pre><p style="width:90%">
	If you then access it with your browser, you should see a
	blank page with the text "Hello World" (without the quotes) on
	it.
      </p></div><div class="example"><a name="table"></a><p class="title"><b>Example2.Generate a Table</b></p><p style="width:90%">
	In another simple example, we dynamically generate a table:
      </p><pre style="background:#bbffbb ; width:90ex ; margin: 2ex ;      padding: 1ex; border: solid black 1px ; white-space: pre;      font-family:monospace ; " class="programlisting">&lt;? puts "&lt;table&gt;\n"
for {set i 1} { $i &lt;= 8 } {incr i} {
    puts "&lt;tr&gt;\n"
    for {set j 1} {$j &lt;= 8} {incr j} {
        set num [ expr $i * $j * 4 - 1]
        puts [ format "&lt;td bgcolor=\"%02x%02x%02x\" &gt; $num $num $num &lt;/td&gt;\n" \
		   $num $num $num ]
    }
    puts "&lt;/tr&gt;\n"
}
puts "&lt;/table&gt;\n" ?&gt;
</pre><p style="width:90%">
	If you read the code, you can see that this is pure Tcl.  We
	could take the same code, run it outside of Rivet, and it
	would generate the same HTML!
      </p><p style="width:90%">
	The result should look something like this:
      </p><div><img src="table.png"></div></div><div class="example"><a name="variable_access"></a><p class="title"><b>Example3.Variable Access</b></p><p style="width:90%">
	Here, we demonstrate how to access variables set by GET or
	POST operations.
      </p><p style="width:90%">
	Given an HTML form like the following:
      </p><pre style="background:#bbffbb ; width:90ex ; margin: 2ex ;      padding: 1ex; border: solid black 1px ; white-space: pre;      font-family:monospace ; " class="programlisting">     &lt;form action="vars.rvt"&gt;
      &lt;table&gt;
	&lt;tbody&gt;
	  &lt;tr&gt;
	    &lt;td&gt;&lt;b&gt;Title:&lt;/b&gt;&lt;/td&gt;
	    &lt;td&gt;&lt;input name="title"&gt;&lt;/td&gt;
	  &lt;/tr&gt;
	  &lt;tr&gt;
	    &lt;td&gt;&lt;b&gt;Salary:&lt;/b&gt;&lt;/td&gt;
	    &lt;td&gt;&lt;input name="salary"&gt;&lt;/td&gt;
	  &lt;/tr&gt;
	  &lt;tr&gt;
	    &lt;td&gt;&lt;b&gt;Boss:&lt;/b&gt;&lt;/td&gt;
	    &lt;td&gt;&lt;input name="boss"&gt;&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;
	    &lt;td&gt;&lt;b&gt;Skills:&lt;/b&gt;&lt;/td&gt;
	    &lt;td&gt;
	      &lt;select name="skills" multiple="multiple"&gt;
		&lt;option&gt;c&lt;/option&gt;
		&lt;option&gt;java&lt;/option&gt;
		&lt;option&gt;Tcl&lt;/option&gt;
		&lt;option&gt;Perl&lt;/option&gt;
	      &lt;/select&gt;
	    &lt;/td&gt;
	  &lt;/tr&gt;
	  &lt;tr&gt;
	    &lt;td&gt;&lt;input type="submit"&gt;&lt;/td&gt;
	  &lt;/tr&gt;
	&lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/form&gt;
</pre><p style="width:90%">
	We can use this Rivet script to get the variable values:
      </p><pre style="background:#bbffbb ; width:90ex ; margin: 2ex ;      padding: 1ex; border: solid black 1px ; white-space: pre;      font-family:monospace ; " class="programlisting">&lt;?
set errlist {}
if { [var exists title] } {
    set title [var get title]
} else {
    set errlist "You need to enter a title"
}

if { [var exists salary] } {
    set salary [var get salary]
    if { ! [string is digit $salary] } {
	lappend errlist "Salary must be a number"
    }
} else {
    lappend errlist "You need to enter a salary"
}

if { [var exists boss] } {
    set boss [var get boss]
} else {
    set boss "Mr. Burns"
}

if { [var exists skills] } {
    set skills [var list skills]
} else {
    lappend errlist "You need to enter some skills"
}

if { [llength $errlist] != 0 } {
    foreach err $errlist {
	puts "&lt;b&gt; $err &lt;/b&gt;"
    }
} else {
    puts "Thanks for the information!"
    ?&gt;
    &lt;table&gt;
      &lt;tbody&gt;
	&lt;tr&gt;
	  &lt;td&gt;&lt;b&gt;Title:&lt;/b&gt;&lt;/td&gt;
	  &lt;td&gt;&lt;? puts $title ?&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
	  &lt;td&gt;&lt;b&gt;Boss:&lt;/b&gt;&lt;/td&gt;
	  &lt;td&gt;&lt;? puts $boss ?&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
	  &lt;td&gt;&lt;b&gt;Salary:&lt;/b&gt;&lt;/td&gt;
	  &lt;td&gt;&lt;? puts $salary ?&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
	  &lt;td&gt;&lt;b&gt;Skills:&lt;/b&gt;&lt;/td&gt;
	  &lt;td&gt;&lt;? puts $skills ?&gt;&lt;/td&gt;
	&lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;?
}
?&gt;
</pre><p style="width:90%">
	The first statement checks to make sure that the
	<tt class="varname">boss</tt> variable has been passed to the
	script, and then does something with that information.  If
	it's not present, an error is added to the list of errors.
      </p><p style="width:90%">
	In the second block of code, the variable
	<tt class="varname">salary</tt> is fetched, with one more error
	check - because it's a number, it needs to be composed of
	digits.
      </p><p style="width:90%">
	The <tt class="varname">boss</tt> variable isn't required to have
	been sent - we set it to "Mr. Burns" if it isn't among the
	information we received.
      </p><p style="width:90%">
	The last bit of variable handing code is a bit trickier.
	Because <tt class="varname">skills</tt> is a listbox, and can
	potentially have multiple values, we opt to receive them as a
	list, so that at some point, we could iterate over them.
      </p><p style="width:90%">
	The script then checks to make sure that
	<tt class="varname">errlist</tt> is empty and outputting a thankyou
	message.  If <tt class="varname">errlist</tt> is not empty, the list
	of errors it contains is printed.
      </p></div><div class="example"><a name="upload"></a><p class="title"><b>Example4.File Upload</b></p><p style="width:90%">
	The following HTML in one file, say,
	<tt class="filename">upload.html</tt>
      </p><pre style="background:#bbffbb ; width:90ex ; margin: 2ex ;      padding: 1ex; border: solid black 1px ; white-space: pre;      font-family:monospace ; " class="programlisting">&lt;form action="foo.rvt" enctype="multipart/form-data" method="post"&gt;
&lt;input type="file" name="MyUpload"&gt;&lt;/input&gt;
&lt;input type="submit" value="Send File"&gt;&lt;/input&gt;
&lt;/form&gt;
</pre><p style="width:90%">
	Can be used with the following Tcl code, in a second file
	(<tt class="filename">upload.rvt</tt> for instance)
	in order to create a file upload form.
      </p><pre style="background:#bbffbb ; width:90ex ; margin: 2ex ;      padding: 1ex; border: solid black 1px ; white-space: pre;      font-family:monospace ; " class="programlisting">&lt;?
upload save MyUpload /tmp/uploadfiles/file1
puts "Saved file [upload filename MyUpload] \
	([upload size MyUpload] bytes) to server"
?&gt;</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="commands.en.html#id4764506"><img src="images/prev.png" alt="Prev"></a></td><td width="20%" align="center"><a accesskey="u" href="index.en.html"><img src="images/up.png" alt="Up"></a></td><td width="40%" align="right"><a accesskey="n" href="tcl_packages.en.html"><img src="images/next.png" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">unescape_string</td><td width="20%" align="center"><a accesskey="h" href="index.en.html"><img src="images/home.png" alt="Home"></a></td><td width="40%" align="right" valign="top">Rivet Tcl Packages</td></tr></table></div></body></html>