Simple Template Engine written in PHP
- Initialization
- Parsing a template
- Assigning and using variables
- Using session variables
- CSS and JS files
With initializing the Engine you also set the Template-Directory. The Template Directory is the directory where the Engine will search for the Files to parse (the template files). If you don't enter a template path the templates will be searched relative to the current path.
E.g.
$engine = new SimpleTemplateEngine();
Will initialize the Engine with the current Directory as the Template-Directory.
$engine = new SimpleTemplateEngine( "./templates" );
Will initialize the Engine with the given template path. When parsing templates they will be expected relative to the given path.
For getting a result out of the engine one must parse a template file.
echo $engine->parse( "myTemplate.html" );
This will trigger the parsing process within the engine. Here myTemplate.html
has to be a file inside the template
-Directory which was set with the initialization. The parsed Result will be returned as a string.
Assigning a variable is done by the assign( $key, $value )
-Method.
E.g. the following php-script
$myVariable = "This is a dummy text.";
$engine->assign( 'dummyText', $myVariable );
when parsed with this Template
<div>{$dummyText}</div>
Will result in
<div>This is a dummy text.</div>
One is not limited in assigning String values. One also can assign objects and access fields or functions:
<!-- Accessing a field -->
{$object->myField}
<!-- Accessing a function return value -->
{$object->function()}
Accessing an array can be done via the arrow notation ->
.
$array = array( "value" );
//or
$array = array( "key" => "value" );
$engine->assign( "array", $array );
Can be accessed like this
<div>{$array->0}</div> <!-- for the indexed array -->
<div>{$array->key}</div> <!-- for the associative array-->
Chaining is not supported.
If a session was started when the engine is initialized a session variable will be automatically set and can be accessed in the template through {$SESSION->identifier}
The engine provides the possibility to add paths of CSS and JS files.
Adding a css path is done with $engine->addCSSFilePath( "/path/to/style.css" )
which adds "/path/to/style.css"
to the List of *.css files.
In your template file you then can use {foreachcss}
to include the css files.
E.g.
Adding paths to the Engine:
$engine->addCSSFilePath( "path/to/firstCSSFile.css");
$engine->addCSSFilePath( "path/to/secondCSSFile.css");
Template (not that {__CSS} is a special variable which you don't have to declare here):
<html>
<head>
{foreachcss}
<link rel="stylesheet" type="text/css" href="{__CSS}">
{/foreachcss}
</head>
</html>
Will result in the output:
<html>
<head>
<link rel="stylesheet" type="text/css" href="path/to/firstCSSFile.css">
<link rel="stylesheet" type="text/css" href="path/to/secondCSSFile.css">
</head>
</html>
For JS simply use $engine->addJSFilePath( $path )
to set the paths and {foreachjs}...{/foreachjs}
in the template file.