tkself

Tutorial:

Using PHPTAL:

Template languages distinguish between push and pull methods: pull means that the template requests information from the backend-application like a client that communicates with a server.

In my opinion this is the wrong way, templates should work in push mode: the application has to prepare all data a priori before sending them to the template-engine for rendering (see also Terence Parr about StringTemplate).

So in our simple tutorial-application we will only use the push mode, precomputing all data before rendering a template with PHPTAL.

At first we modify the template file (tmpl/phptal/tutorial_base.tpl) to prepare places to be substituted with data send from the backend (the title-tag and the content):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title tal:context/title="title">untitled</title>
</head>
<body>
    <div class="content" 
         tal:content="context/content">
         content to be substituted
    </div>
</body>
</html>

Reloading the page now will raise a PHPTAL_VariableNotFoundException from PHPTAL looking more or less like the following output depending on the filepaths:

PHPTAL tries to read the value from $context['title'] to insert this as the content of the title-tag, but this variable is not defined. Let's define this variable now and edit the view-file views/tutorial.php, adding the $context-array:

<?php

function view($request_info) {
    $context = array();
    $context["title"] = 'tk_self tutorial';
    $context["content"] = 'this is the content';
    return render_to_response('tutorial_base.tpl'$context);
}

?>

It is important to define all variables accessed by PHPTAL to avoid an exception. One of the advantages of the way PHPTAL works is that a webdesigner can work with WYSIWYG-tools on PHPTAL templates while the programmer can work on subsituting the content. In the above code $context is defined as an assioziative array with all keys that will be called from PHPTAL. Also $context is given as second parameter to render_to_response() to give PHPTAL access to the array. Reloading the page now will render without any error:

Contact | Impressum | License