Creating a Simple View

Let's start with a simple example. First we need to create our view handling class:

SimpleView.php
<?php
require_once 'HTML/Template/Nest/View.php';

HTML_Template_Nest_View::$CACHE = false;

$view = new HTML_Template_Nest_View("simpleview");
print $view->render();

Go ahead and create a 'views' directory in the same folder as SimpleView.php. In the views folder create:

simpleview.nst
<html>
    <head>
        <title>A Simple View</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

That's it. Point your web browser at SimpleView.php and you should see your Hello World page.

Just a quote note about that $CACHE flag. By default Nest doesn't cache output pages. In production you'll want to set the $CACHE flag to true to have it compile once and then reuse that file.

Adding a variable to your view

At this point we've just displayed an HTML document, which we could have done easily without the need for a specialized library. So let's add a line to our SimpleView.php file:

SimpleView.php
<?php
require_once 'HTML/Template/Nest/View.php';

HTML_Template_Nest_View::$CACHE = false;

$view = new HTML_Template_Nest_View("simpleview");
$view->addAttribute("username", "Tim");
print $view->render();
Now let's change the nest template:
simpleview.nst
<html>
    <head>
        <title>A Simple View</title>
    </head>
    <body>
        <h1>Hello ${username}!</h1>
    </body>
</html>

That's it. Values can be displayed in the view by simply adding them as attributes and then inserting them as a token into the nest template. If you view SimpleView.php in a web browser you should see "Hello Tim!".