Class Template
A simple templating engine. This is a simple example of how to use this class:
$t=new Template("templatefile.php");
$t->set("hello","hello world");
$t->show();
In this example, we have a template called templatefile.php
that gets loaded
and rendered using the show
call. The variables registered using the set
method
will be made available to the template in the global scope. The contents of
the templatefile.php
for this example could be:
<html>
<body>
We say hello like this: <?php echo $hello; ?>
</body>
</html>
Methods summary
public
|
#
__construct( mixed $filename )
Create a Template for the specified file.
Create a Template for the specified file.
Parameters
- $filename
- The file to use as template.
|
public
|
#
set( mixed $name, mixed $value )
Set a variable that can be accessed by the template.
Set a variable that can be accessed by the template.
The variable will be available to the template in the global scope.
Parameters
- $name
- The name of the variable.
- $value
- The value that the variable should take.
|
public
|
#
show( )
Render the template and output it to the browser.
Render the template and output it to the browser.
|
public
|
#
render( )
Render template, but don't ouput it to the browser.
Render template, but don't ouput it to the browser.
This is useful if we want to
use a template inside another template. For example, we might have a
page template that defaines header and footer for our page. Inside the page
template we want to display the content generated by a content template.
We can do this by first rendering the content template:
$contentTemplate=new Template("the_content_templte.php");
$content=$contentTemplate->render();
Now, we use the rendered content as input for our page template and output
everything to the browser:
$pageTemplate=new Template("the_page_template.php");
$pageTemplate->set("content",$content);
$pageTemplate->show();
|