Hy! I am kinda stuck in a problem. Actualy, i am donig some upgrades in my cms, and i hope you can help me. My templating system is replacing some tpl variables with data. Variables are in format {@Name}, etc...
What I need is to parse .tpl file and put all of theese variables in an array, but without "{@" and "}". Just name or surname or whatever is init. Can anybody help?
[SOLVED]tpl parsing
Moderator: General Moderators
- MindOverBody
- Forum Commoner
- Posts: 96
- Joined: Fri Aug 06, 2010 9:01 pm
- Location: Osijek, Croatia
[SOLVED]tpl parsing
Last edited by MindOverBody on Mon Aug 09, 2010 10:10 pm, edited 1 time in total.
- MindOverBody
- Forum Commoner
- Posts: 96
- Joined: Fri Aug 06, 2010 9:01 pm
- Location: Osijek, Croatia
Re: tpl parsing
I played a bit, and came up with solution, wrote whole class for solution, and here it is... maybe someone find it somehow usefull. 
ps. sry for missthread.
Class:
Example:
Loaded template.tpl.php file:
Result:
ps. sry for missthread.
Class:
Code: Select all
<?php
/**
* File parser
*/
class Parser {
/**
* Search results after preg_match
* @var array
* @access protected;
*/
protected $SearchResults;
/**
* Targeted file
* @var string
* @access protected
*/
protected $File;
/**
* Array of final unique variables
* @var array
* @access protected
*/
protected $AdaptedVars;
/**
* Regex search pattern
* @var string
* @access protected
*/
protected $SearchPattern;
/**
* Constructor method
* @access public
*/
public function __construct(){
// set SearchResults as array
$this -> SearchResults = array();
// set AdaptedVars as array
$this -> AdaptedVars = array();
}
/**
* File load method
* @param string $Filename File name of a targeted file
* @access public
*/
public function Load ( $Filename ){
$this -> File = $Filename;
}
/**
* This method set search patern variable
* @param string $value Search pattern string
* @access public
*/
public function setSearchPattern( $value ){
$this -> SearchPattern = $value;
}
/**
* This method gets content of a given file, and search for given pattern.
* Calls adepting method and finaly returns final array of results
* @return array
* @access public
*/
public function Process(){
// get content of a file
$Content = file_get_contents ( $this -> File );
// do preg_match magic
preg_match_all( "/" . $this -> SearchPattern . "/" , $Content, $this -> SearchResults, PREG_PATTERN_ORDER );
// adapt results
$this -> adaptResults();
// return array of results
return $this -> AdaptedVars;
}
/**
* Method which search if variable already set in final array
* If variable is already in final array, returns 1
* @return bool
* @access private
*/
private function searchTwin( $var ){
// first count if array have any element, if not return 0
if ( count ( $this -> AdaptedVars ) == 0 ) return false;
// else search whole array for given value
else foreach( $this -> AdaptedVars as $cand ) if ( $var == $cand )return true;
}
/**
* This method do adepting process over preg_mathc array, checks if variable is already registered,
* and adds to final array if not. Also removes '{@' and '}' strings
* @access private
*/
private function adaptResults(){
// for each result element do adaption
foreach ( $this -> SearchResults[0] as $Variable){
$Variable = str_replace( "{@", "", $Variable );
$Variable = str_replace( "}", "", $Variable );
// search if current result element is
// already in final array
if ( $this -> searchTwin( $Variable ) == false ){
// if not, push it into array
array_push( $this -> AdaptedVars, $Variable );
}
}
}
}
?>
Code: Select all
$Parser = new Parser;
$Parser -> Load ('template.tpl.php');
$Parser -> setSearchPattern( "{@.[a-zA-Z0-9]{1,}}" );
$data = $Parser -> Process();
foreach ( $data as $var ){
echo $var . '<br>';
}
Code: Select all
<!-- Latest Article item -->
<div class='latest-article-item'>
<div class='latest-article-item-position'>In <strong>{@ArticleSection}</strong> / {@ArticleCategory}</div>
<h1><a href='index.php?{@Object}={@ObjectValue}&{@IDObject}={@IDValue}'>{@ArticleCaption}</a></h1>
<div class='latest-article-item-date'> Objavljeno {@ArticleDate}</div>
<div class='latest-article-item-text'>{@ArticleText}</div>
</div>
<!-- End of Latest Article item -->
Code: Select all
ArticleSection
ArticleCategory
Object
ObjectValue
IDObject
IDValue
ArticleCaption
ArticleDate
ArticleText