To set up a project you have to
In this tutorial PHPTAL will be used as template engine. To configure this open the file conf/tk_settings.php and decomment the definition to use PHPTAL:
// define the template engine to use: // you can choose between php, phptal and smarty //define ("TK_SELF_TEMPLATE_ENGINE", TK_SELF_TE_PHP); define ("TK_SELF_TEMPLATE_ENGINE", TK_SELF_TE_PHPTAL); //define ("TK_SELF_TEMPLATE_ENGINE", TK_SELF_TE_SMARTY);
After saving the file and reloading the page http://tutorial.tkself.test/ an error-message may occure, stating that the file PHPTAL.php is missing:
Warning: require_once(../phptal/PHPTAL.php) [function.require-once]: failed to open stream: No such file or directory in [...]/lib/tk_http.php on line 26
In this case take care to install PHPTAL as described in Installation.
If PHPTAL is installed you will get another error-message from PHPTAL that the template file 'welcome.tpl' could not be found. This message will start with:
exception 'PHPTAL_Exception' with message 'Unable to locate template file ../tmpl/phptal/welcome.tpl' in ...
At this moment we don't have any template file written to be used by PHPTAL. Furthermore the default view-function is called which assumes that a PHP template file of this name should be used. So at next we will change the URL-pattern, write the corresponding view-function and set up a template file to be used from this view-function.
Open the file conf/tk_urlpatterns.php and set the first entry so, that all URLs will be handled from the function view in the tutorial.php file:
$TK_SELF_URL_PATTERNS = array( "^" => "tutorial.view", // "^$" => "test.welcome", "^test/?$" => "test.view",
A try to reload the page now will result in another error-message, because the file views/tutorial.php with the view-function view is not existent:
Warning: require_once(../views/tutorial.php) [function.require-once]: failed to open stream: No such file or directory in ..
So at next we will write this view-function.
Create the file views/tutorial.php with the following content:
<?php function view($request_info) { return 'this is the tutorial'; } ?>
A reload of the page will now print the sentence 'this is the tutorial'. The view-function has to return the HTML-code of the page which is just plain text in this case, but this is rendered by the browser anyway.

If you rename the view-function view to i.e. render, the URL will not point to an existing view/function anymore and another error-message is raised:
Fatal error: Call to undefined function view() in ...
These are the typical error-messages you may get from tk_self by wrong settings or wrong URL patterns.
At next we will set up a template file for output. Create the following file
<!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>untitled</title> </head> <body> tutorial HTML-output </body> </html>
and store it as tmpl/phptal/tutorial_base.tpl. To use this template modify the code of the view-function to:
<?php function view($request_info) { return render_to_response('tutorial_base.tpl'); } ?>
A reload of the page will now print the sentence 'tutorial HTML-output'.

But now the output is a HTML-page (as you can see by the title 'untitled' or by viewing the page-source) because the function render_to_response('tutorial_base.tpl') calls the configured template engine to return the rendered template file tutorial_base.tpl given as argument. In this example nothing is substituted by the template engine, so the template is returned 'as is'.
What we have done so far are
The next chapter will show, how tk_self and PHPTAL will play together.