Don't create a parser as its totally pointless, PHP is already a template engine, all you want to do is extend its capabilities with encapsulated template system.
The method below is the way I always create my template engine, its very lose, and can be extended with template helpers as such.
a good thing about my template system is that you don't have to waste your resources creating a cache system for your compiled templates as there already in a compiled state, comparing to engines like smarty.
Firstly we want to create a the main template class which would be used for setting data, and selecting the template etc.
So lets get started with this:
class Template
{
private $__data = array();
public function __construct(){/*Set up template root here*/}
public function __set($key,$val)
{
$this->__data[$key] => $val;
}
public function __get($key)
{
return $this->__data[$key];
}
public function Display($Template)
{
new TemplatePage($Template,$this->__data); //Send data and template name to encapsulated object
}
}
this is a very basic class that will allow is do do something like
$Template = new Template();
$Template->Userdata = $User->GetUserData();
$Template->Display("frontend/index");
Ok now you might have noticed the class above called TemplatePage, this is the class that loads the actual file template, and within the actual template you would be in the scope of the TemplatePage scope.
This will will allow you to have methods to get your template data and also access helpers, below is an example of a TemplatePage
class TemplatePage
{
private $__data = array();
public function __construct($template,$data)
{
$this->__data = $data;
unset($data);
//Load the templates
if(file_exists(TEMPLATE_PATH . "/" . $template . ".php"))
{
require_once TEMPLATE_PATH . "/" . $template . ".php";
return;
}
trigger_error("Template does not exists",E_USER_ERROR);
}
public function __get($key)
{
return $this->__data[$key];
}
public function require($template)
{
if(file_exists(TEMPLATE_PATH . "/" . $template . ".php"))
{
require_once TEMPLATE_PATH . "/" . $template . ".php";
return;
}
trigger_error("Template does not exists",E_USER_NOTICE);
}
public function link($link,$title)
{
return sprintf('<a href="%s">%s</a>',$link,$title);
}
}
So now as the template is loaded within the scope of the class you can extend the methods such as link and require to have a fully function template tool-kit
Example of template:
<?php $this->require("folder/js_modules") ?>
<ul>
<?php foreach ($this->photos as $photo): ?>
<li>
<?php echo $this->link($photo->link,"click to view") ?>
<span><?php echo $photo->image_name?></span>
</li>
<?php endforeach; ?>
</ul>
<?php $this->require("folder/footer") ?>
<?=$(or<?php echo $into{{and ` ?>` into}}? You're just creating dozens of new problems rather solving any.