SourceForge.net Logo
Home | SourceForge | Documentation | FAQ | What's New | To-Do List | Forums | Download | Contact

Sparse - Templates (Overview)

Although there are three main types of templates used by Sparse, each of them shares the same basic layout. You can have any amount of HTML tags between the start and end of your HTML file, and you can also have one or more templates in the same file. Templates are defined as being inside sqltemplate tags. So, your file could have two templates in it, and look like this:

<?php
  include('Sparse.php');
  SparseThisPage('username', 'password');
?>
<html>
  <!-- some HTML code goes here -->
  <sqltemplate ...>
    <!-- HTML and Sparse tags go here -->
  </sqltemplate>
  <!-- more HTML code goes here -->
  <sqltemplate ...>
    <!-- More HTML and Sparse tags go here -->
  </sqltemplate>
</html>

Now let's take a look at the tags that go inside the individual templates. The first important tag is sqlrow. There must be exactly one of these tags inside every template. The code inside the row is repeated for every row that's displayed - this can be the data that's displayed, or new rows to enter for add templates. Most Sparse tags must either appear inside the sqlrow (meaning it also would be repeated per row) or outside it (meaning it would not be repeated) but not both.

The second important tag is sqlfield. Without this tag you couldn't do anything with the data! sqlfield tags indicate where you want the data to go - again, either the existing data, or a place to enter new data (or, when editing data, both!).

The final important tag is sqlaction. These tags are replaced by submit buttons depending on where they are and what kind of template they're in.

Here's a simple example of how one might go about displaying the contents of a table. The following code will print out the entire contents of the "employees" table, showing the employee name, ID, and salary, and allowing the user to edit those values:

<?php
  include('Sparse.php');
  SparseThisPage('username', 'password');
?>
<html>
  <h2 align="center">Employee Database</h2>
  <table>
    <tr>
      <th>Employee Name</th>
      <th>Employee ID</th>
      <th>Employee Salary</th>
    </tr>
    <sqltemplate type="edit" database="myDB" tables="employees">
        <sqlrow>
          <tr>
            <td><sqlfield name="employeeName" /></td>
            <td><sqlfield name="employeeID" /></td>
            <td><sqlfield name="employeeSalary" /></td>
          </tr>
        </sqlrow>
        </table>
    <sqlaction type="edit" />
    </sqltemplate>
</span>

Previous: Introduction | Templates - Overview | Next: Templates and Queries