File: examples.it.html

package info (click to toggle)
rivet 0.5.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,664 kB
  • ctags: 1,359
  • sloc: sh: 8,342; xml: 6,622; ansic: 4,367; tcl: 2,813; makefile: 99; lisp: 78; sql: 25
file content (181 lines) | stat: -rw-r--r-- 9,704 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
175
176
177
178
179
180
181
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Esempi ed uso</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.it.html" title="Apache Rivet"><link rel="up" href="index.it.html" title="Apache Rivet"><link rel="prev" href="commands.it.html" title="Comandi e variabili Tcl di Rivet"><link rel="next" href="tcl_packages.it.html" title="Pacchetti Tcl per Rivet"></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">Esempi ed uso</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="commands.it.html"><img src="images/prev.png" alt="Indietro"></a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="tcl_packages.it.html"><img src="images/next.png" alt="Avanti"></a></td></tr></table></div><div class="section" lang="it"><div class="titlepage"><div><div><hr><h2 class="title" style="clear: both"><a name="examples"></a>Esempi ed uso</h2></div></div></div><p style="width:90%">
      Seguono alcuni esempi sull'uso di Rivet.  Si presuppone che 
      conosciate, anche parzialmente, il linguaggio di programmazione 
      Tcl. Se non conoscete molto Tcl, non c' da preoccuparsi:  
      semplice e vi sono alcune buone risorse disponibili
      sul web che vi permetteranno di padroneggiarlo velocemente. 
      A questo proposito, vedete la sezione 
      <a href="help.it.html#websites" title="Siti Web">web sites</a>.
    </p><div class="example"><a name="hello world"></a><p class="title"><b>Esempio1.Hello World</b></p><p style="width:90%">
	Come qualsiasi altro tool,  piacevole vedere qualcosa al 
	lavoro, cos andremo a creare una piccola pagina 
	"Hello World".</p><p style="width:90%">
	Assumendo che sia stato configurato correttamente Apache, 
	create un file chiamato <tt class="filename">hello.rvt</tt> in una 
	directory dove Apache lo possa trovare, con il seguente 
	contenuto:
      </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%">
	Se poi vi accederete con vostro browser, sar possibile vedere 
	una pagina nera con il testo "Hello World" (senza apici).
      </p></div><div class="example"><a name="table"></a><p class="title"><b>Esempio2.Generare una tabella</b></p><p style="width:90%">
	In un'altro semplice esempio, genereremo dinamicamente una
	tabella:
      </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%">
	Se leggete il codice,  possibile vedere che  puro Tcl. 
	possibile mantenere lo stesso codice, eseguirlo fuori da Rivet
	e generare la stessa pagina HTML!
      </p><p style="width:90%">
	Il risultato sar qualcosa di simile a questo:
      </p><div><img src="table.png"></div></div><div class="example"><a name="variable_access"></a><p class="title"><b>Esempio3.Accesso alle variabili</b></p><p style="width:90%">
	In questa sezione mostreremo come accedere alle variabili 
	utilizzando gli operatori GET o POST.
      </p><p style="width:90%">
	Data una form HTML come la seguente:
      </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%">
	Potete utilizzare questo script di Rivet per ottenere i valori
	delle variabili:
      </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%">
	La prima istruzione si assicura che la variabile
	<tt class="varname">boss</tt> sia passata allo script e poi fa
	qualcosa con quella informazione.
	Se non  presente, un errore viene aggiunto alla lista degli 
	errori.
      </p><p style="width:90%">
	Nel secondo blocco del codice, la variabile
	<tt class="varname">salary</tt>  riportata con un'altro errore di
	controllo, poich  un numero,  necessario che sia composto
	da cifre.
      </p><p style="width:90%">
	Alla variabile <tt class="varname">boss</tt> non  richiesto di essere
	inviata, la setteremo a "Mr. Burns" se non  tra le informazioni
	ricevute.
      </p><p style="width:90%">
	L'ultimo pezzo del codice di gestione delle variabili  un pezzo
	ingannatore perch <tt class="varname">skills</tt>  una listbox e pu
	avere, potenzialmente, valori multipli. Opteremo di riceverla
	come una lista, in modo da poterla riutilizzare.
      </p><p style="width:90%">
	Lo script si assicura che la variabile 
	<tt class="varname">errlist</tt> sia vuota e restituisce un messaggio 
	di ringraziamento. Se <tt class="varname">errlist</tt> non  vuota,
	viene stampata la lista degli errori riscontrati.
      </p></div><div class="example"><a name="upload"></a><p class="title"><b>Esempio4.Caricare file</b></p><p style="width:90%">
	Il seguente HTML in un file, detto,
	<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%">
	Pu essere usato con il seguente codice Tcl in un secondo file
	(<tt class="filename">upload.rvt</tt> per esempio) per creare una 
	form che carica un file.
      </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.it.html"><img src="images/prev.png" alt="Indietro"></a></td><td width="20%" align="center"><a accesskey="u" href="index.it.html"><img src="images/up.png" alt="Risali"></a></td><td width="40%" align="right"><a accesskey="n" href="tcl_packages.it.html"><img src="images/next.png" alt="Avanti"></a></td></tr><tr><td width="40%" align="left" valign="top">Comandi e variabili Tcl di Rivet</td><td width="20%" align="center"><a accesskey="h" href="index.it.html"><img src="images/home.png" alt="Partenza"></a></td><td width="40%" align="right" valign="top">Pacchetti Tcl per Rivet</td></tr></table></div></body></html>