Throughout this tutorial, the term "controller logic" will refer to the part of your PHP application that instantiates Savant and assigns variables to it, and the term "view logic" will refer to the template that takes the variables and displays them to the user.

When using Savant, you will always have two separate files, generally a ".php" script (the controller logic that manipulates data) and a ".tpl.php" script of the same base name (the view logic that formats the data for display).

Controller Logic: books.php

This is the business logic script; it creates or accesses data, manipulates the data, and then sends the data to the template. You could call it "books.php".


// Load the Savant3 class file and create an instance.
require_once 'Savant3.php';
$tpl = new Savant3();

// Create a title.
$name "Some Of My Favorite Books";

// Generate an array of book authors and titles.
$booklist = array(
'author' => 'Hernando de Soto',
'title' => 'The Mystery of Capitalism'
'author' => 'Neal Stephenson',
'title' => 'Cryptonomicon'
'author' => 'Milton Friedman',
'title' => 'Free to Choose'

// Assign values to the Savant instance.
$tpl->title $name;
$tpl->books $booklist;

// Display a template using the assigned values.

View Logic: books.tpl.php

This is the template script; it takes the data passed to it by the controller logic and formats it for display. The following code is the "books.tpl.php" file called by $tpl->display() above.


        <title><?php echo $this->eprint($this->title); ?></title>

        <?php if (is_array($this->books)): ?>
            <!-- A table of some books. -->
                <?php foreach ($this->books as $key => $val): ?>
                        <td><?php echo $this->eprint($val['author']); ?></td>
                        <td><?php echo $this->eprint($val['title']); ?></td>
                <?php endforeach; ?>
        <?php else: ?>
            <p>There are no books to display.</p>
        <?php endif; ?>


  1. We use normal PHP as the template language, and use the $this->eprint() method to display variable values whenever possible. The $this->eprint() method automatically escapes output, which helps to avoid cross-site scripting attacks. You may use echo and print as well, if you wish.
  2. Variables assigned by the controller logic are referred to with "$this->varname" notation, while variables created within the template are referred to as "$varname" -- this helps to keep track of what came from the controller and what is being used specifically for the view logic.
  3. We use the alternative syntax for control structures; that is, "if (): ... endif;" instead of "if () {...}". While not required, it can be easier to read in many cases.