I am building a PHP framework and would like to get some feedback on a few different section of the project so far. I consider myself still a neophyte in PHP so I would like to ask if I'm going about completing this different task in an efficient and or correct way.
This section is of the Template parser. I wish to create a parser that will take html files with only tags [@tag] and no php and have it parse the file replacing the tags with supplied data and return it if it need to be used in the parsing of another template.
From research and a personal idea I thought it would be best to try and divide the parsing mechanics and the injecting of dynamic data/info. So I created two classes the Page class and Template class. The template class over sees the parsing of the html files and the page over sees that tag replacement.
Template Class (instantiates the page class)
template.object.php
class Template{
private $appath;
private $page;
protected $settings;
private $issettings;
public $tags = array();
public $store = array();
public $template = array();
//Set path for templat use
public $path;
/**
* Set app_path, include page class
* Create Page instance
*/
public function __construct(){
$this->appath = APP_PATH;
require_once(dirname(__FILE__).'/'.'page.object.php');
$this->page = new Page();
}
/**
* Set the path to template file
* values are added to an array
* @param Int $tempkey - Key value to be used to identify value
* @return Int $tempkey used
*/
public function SetTemplateFile($tempkey,$file){
if(isset($this->settings)){
$template = $this->appath.'/styles/'.$this->settings.'/templates/'.$file.'.template.html';
if(file_exists($template)){
$this->template[$tempkey] = $template;
return $tempkey;
}else{
trigger_error('Temple file not found',E_USER_ERROR);
}
}
}
/**
* Retrieves set template files
* Note: Retrieved from an array Key identifier needed
* @param String or Int $key - Index key used to
* @return Int $tempkey used
*/
public function GetTemplateFile($key){
if(array_key_exists($key,$this->template)){
return $this->template[$key];
}else{
trigger_error('Template File key not found',E_USER_ERROR);
}
}
/**
* Manually set temple block tags content
* Note: Manually add content to a section of the template
* @param String $tags - the template tag to place $value
* @param String $value - content to replace $tag
* @return String of the tag selected
*/
public function SetTags($tags,$value){
$this->tags[$tags] = $value;
return $tags;
}
/**
* Set a group of tags with using an array
* Note: meant to use a multidimensional array
* @param Array $array - multidimensional array with the tag as the key and value as the tag value
* @return Null
*/
public function SetGroupTags($array){
if(is_array($array)){
foreach ($array as $subarray) {
foreach ($subarray as $key => $value) {
$this->tags[$key] = $value;
}
}
}else{
trigger_error('Paramete for not an array '.gettype($array).' type given',E_USER_ERROR);
}
}
/**
* Set a group of tags with using an array
* Note: Meant to work with a multidimensional array
* Stores the data in member variable $store
* @param Array $array - multidimensional array with the tag as the key and value as the tag value
* @return Null
*/
public function SetBlock($tempkey,$array){
if(file_exists($this->template[$tempkey])) {
foreach ($array as $subarray) {
$output = file_get_contents($this->template[$tempkey]);
foreach ($subarray as $key => $value) {
$tagToReplace = "[@$key]";
$output = str_replace($tagToReplace, $value, $output);
}
$this->store[] = $output;
}
}else{
trigger_error('Unable to set template block',E_USER_ERROR);
}
}
/**
* Phrases values stored in member variable $store
* @param Null
* @return String phrased template block
*/
public function RunBlock(){
foreach ($this->store as $value) {
$output .=$value."\n";
}
//Temp FIX
unset($this->store);
return $output;
}
/**
* Set page template tags with meta, script, and style tags value
* @param Null
* @return Null
*/
public function AddHeadFiles(){
$this->tags['meta'] = $this->page->GetMeta();
$this->tags['script'] = $this->page->GetJS();
$this->tags['styles'] = $this->page->GetCSS();
}
/**
* Set page template tags with meta, script, and style tags value
* @param Null
* @return Null
*/
public function Output($key){
if (file_exists($this->template[$key])) {
//Add JavaScripts, StyleSheets and Meta tags info using (PageCon method)
$this->AddHeadFiles();
$output = file_get_contents($this->template[$key]);
foreach ($this->tags as $key => $value) {
$tagToReplace = "[@$key]";
$output = str_replace($tagToReplace, $value, $output);
}
return $output;
}else{
trigger_error('Unable to phrase template block',E_USER_ERROR);
return false;
}
}
/**
* Set the styles directory to use
* Note: Settings is used to reference the directory to use
* when working this a template files
*
* @param String $data - Name of directory of template files
* @return Null
*/
public function SetSettings($data){
if($this->IsSetting($data)){
$this->settings = $data;
//Add the [@path] tag with
//To supply a direct path for the css and js files
$this->tags['path'] = 'styles/'.$data;
//Send setting to PageCon
$this->page->GetTempSet($data);
}else{
trigger_error('Invalid parameter of: ('.$data.')',E_USER_ERROR);
}
}
/**
* Check to see if a directory with the name of the setting valus
*
* @param String $data - setting valus being used
* @return true or Null
*
* If error is caught for the DirIterator throws and exception
*/
public function IsSetting($data){
$this->issettings = $this->appath.'/styles/'.$data;
try{
$dir = new DirectoryIterator($this->issettings);
if($dir->isDir()){
return true;
}
}catch(Exception $e){
echo '<strong>Template Error: in Class Template{} Line '.$e->getLine().'</strong><br />';
}
}
public function __destruct(){
//Empty
}
}
Page Class
page.object.php
class Page{
//Header tags
public $title;
public $meta = array();
public $styles = array();
public $scripts = array();
private $settings;
//Body tage
public $menu = array();
public $bodyTags = array();
public $addBodyTags;
public function __construct(){
//Empty
}
/**
* Set the metatags to be added to the page
* @param Array $array - an array of the meta tags and information
* @return Null
*/
public function SetMeta($array){
foreach ($array as $value) {
foreach ($value as $key => $value) {
$output .= ''.$key.'="'.$value.'" ';
}
$this->meta[] = "<meta $output />";
unset($output);
}
}
/**
* Retrieve the metatag information set
* @param Null
* @return string of metatag information
*/
public function GetMeta(){
foreach ($this->meta as $metatag) {
$output .= $metatag."\n";
}
return $output;
}
/**
* Set template setting for Template class
* @param String $settings - template file to use when generating page
* @return Null
*/
public function GetTempSet($settings){
$this->settings = $settings;
}
/**
* An array of JavaScript files
* @param Array $array - js files to add
* @return Null
*/
public function AddJS($array){
$arg = explode(',',$array);
for ($i = 0; $i < count($arg); $i++){
if(strpos($arg[$i],'.js')){
$this->scripts[] = '<script src="styles/'.$this->settings.'/js/'.$arg[$i].'" type="text/javascript"></script>';
}else{
$this->scripts[] = '<script src="styles/'.$this->settings.'/js/'.$arg[$i].'.js" type="text/javascript"></script>';
}
}
}
/**
* Retrieve a list of the added JavaScript files to include into the page
* @param Null
* @return String list of js files
*/
public function GetJS(){
foreach ($this->scripts as $script) {
$output .= $script."\n";
}
return $output;
}
/**
* An array of CSS files
* @param Array $array - css files to add
* @return Null
*/
public function AddCSS($array){
$arg = explode(',',$array);
for ($i = 0; $i < count($arg); $i++){
if(strpos($arg[$i],'.css')){
$this->styles[] = '<link rel="stylesheet" href="styles/'.$this->settings.'/css/'.$arg[$i].'"/>';
}else{
$this->styles[] = '<link rel="stylesheet" href="styles/'.$this->settings.'/css/'.$arg[$i].'.css"/>';
}
}
}
/**
* Retrieve a list of the added CSS files to include into the page
* @param Null
* @return String list of css files
*/
public function GetCSS(){
foreach ($this->styles as $style) {
$output .= $style."\n";
}
return $output;
}
public function __destruct(){
//Empty
}
}
Here is an example of them being used to generate a html list using ulist.template.html and lilist.template.html
HTML files being used in esample
ulist.template.html
<ul>
[@list]
</ul>
lilist.template.html
<li><a href="[@link]">[@text]</a></li>
PHP code EX
//Sample array of data
$array = array(
array('link' => 'http://website1.com','text' => 'Website One'),
array('link' => 'http://website2.net','text' => 'Website Two'),
array('link' => 'http://website3.org','text' => 'Website Three')
);
$template = new Template;
//Set style to use for template
$template->SetSettings('demo');
//Set ulist.template.html as a temp file
//to be used
$ultag = $template->SetTemplateFile('ulist','ulist');
//Set lilist.template.html as a temp file
//to be used
$litag = $template->SetTemplateFile('lilist','lilist');
//Data to be inputed into the temp file
//data used from $array
$template->SetBlock($litag,$array);
//Parse the lilist.template.html file
//with $array data and return parsed temp file
$generatedlilist = $template->RunBlock();
//Add the parsed lilist into ulist.template file
$template->SetTags('list',$generatedlilist);
//Echo generated html list
echo $template->Output($ultag);
It then outputs the html
<ul>
<li><a href="http://website1.com">Website One</a></li>
<li><a href="http://website2.net">Website Two</a></li>
<li><a href="http://website3.org">Website Three</a></li>
</ul>
There a few steps to parsing a template file I'm not sure if that is a bad thing. I find that it makes building a little easier.
Is this a good way to build this type of function/feature? I am open to any tip, tricks, suggestions, and advice.
link
value contains[@text]
? \$\endgroup\$<a href="Website One">Website One</a>
Are you suggestion I make sure tags can not be used as values to be inputted into the template in the parsing process? \$\endgroup\$