Examples and Usage

## Examples and Usage

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 web sites section and have a look.

Example 1. Hello World

As with any tool, it's always nice to see something work, so let's create a small "Hello World" page.

Assuming you have Apache configured correctly, create a file called `hello.rvt` where Apache can find it, with the following content:

    <?
    puts "Hello World"
    ?>

If you then access it with your browser, you should see a blank page with the text "Hello World" (without the quotes) on it.

Example 2. Generate a Table

In another simple example, we dynamically generate a table:

    <? puts "<table>\n"
    for {set i 1} { $i <= 8 } {incr i} {
        puts "<tr>\n"
        for {set j 1} {$j <= 8} {incr j} {
            set num [ expr $i * $j * 4 - 1]
            puts [ format "<td bgcolor=\"%02x%02x%02x\" > $num $num $num </td>\n" \
    		   $num $num $num ]
        }
        puts "</tr>\n"
    }
    puts "</table>\n" ?>

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!

The result should look something like this:

Example 3. Variable Access

Here, we demonstrate how to access variables set by GET or POST operations.

Given an HTML form like the following:

         <form action="vars.rvt">
          <table>
    	<tbody>
    	  <tr>
    	    <td><b>Title:</b></td>
    	    <td><input name="title"></td>
    	  </tr>
    	  <tr>
    	    <td><b>Salary:</b></td>
    	    <td><input name="salary"></td>
    	  </tr>
    	  <tr>
    	    <td><b>Boss:</b></td>
    	    <td><input name="boss"></td></tr>
    	  <tr>
    	    <td><b>Skills:</b></td>
    	    <td>
    	      <select name="skills" multiple="multiple">
    		<option>c</option>
    		<option>java</option>
    		<option>Tcl</option>
    		<option>Perl</option>
    	      </select>
    	    </td>
    	  </tr>
    	  <tr>
    	    <td><input type="submit"></td>
    	  </tr>
    	</tbody>
          </table>
        </form>

We can use this Rivet script to get the variable values:

    <?
    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 "<b> $err </b>"
        }
    } else {
        puts "Thanks for the information!"
        ?>
        <table>
          <tbody>
    	<tr>
    	  <td><b>Title:</b></td>
    	  <td><? puts $title ?></td>
    	</tr>
    	<tr>
    	  <td><b>Boss:</b></td>
    	  <td><? puts $boss ?></td>
    	</tr>
    	<tr>
    	  <td><b>Salary:</b></td>
    	  <td><? puts $salary ?></td>
    	</tr>
    	<tr>
    	  <td><b>Skills:</b></td>
    	  <td><? puts $skills ?></td>
    	</tr>
          </tbody>
        </table>
        <?
    }
    ?>

The first statement checks to make sure that the `boss` 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.

In the second block of code, the variable `salary` is fetched, with one more error check - because it's a number, it needs to be composed of digits.

The `boss` variable isn't required to have been sent - we set it to "Mr. Burns" if it isn't among the information we received.

The last bit of variable handing code is a bit trickier. Because `skills` 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.

The script then checks to make sure that `errlist` is empty and outputting a thankyou message. If `errlist` is not empty, the list of errors it contains is printed.

Example 4. File Upload

The following HTML in one file, say, `upload.html`

    <form action="foo.rvt" enctype="multipart/form-data" method="post">
    <input type="file" name="MyUpload"></input>
    <input type="submit" value="Send File"></input>
    </form>

Can be used with the following Tcl code, in a second file (`upload.rvt` for instance) in order to create a file upload form.

    <?
    upload save MyUpload /tmp/uploadfiles/file1
    puts "Saved file [upload filename MyUpload] \
    	([upload size MyUpload] bytes) to server"
    ?>

unescape_string |  | Rivet Tcl Packages