I am using a template class which is perfect for my needs. The only problem I'm having is that it is suppose to remove placeholders if a value is not set but it's not working.
Here is the code where it is doing it:
/**
* Replace non used elements
* @access private
*/
function _replace_none()
{
$this->_code=ereg_replace("\\[([^\\[]*),([^\\[]*)\\]",'',$this->_code);
$this->_code=ereg_replace("\\[([^\\[]*),([^\\[]*),([^\\[]*)\\]",'',$this->_code);
$this->_code=preg_replace("|<!-- code(.*?)-->|s",$el,$this->_code);
$this->_code=preg_replace("|<!-- end code -->|s",$el,$this->_code);
$this->_Element_replace('replace');
}
/**
* Return HTML code
* @return string $Code Code HTML
* @access public
*/
function toHtml()
{
$this->_replace_none();
return $this->_code;
}
/**
* Print HTML code
* @access public
*/
function display()
{
if(!$this->_linesSet)
return False;
echo $this->toHtml();
}
I do not understand regular expressions so I'm unclear why it's not working.
I call display() which calls toHtml() which calls _replace_none(). _replace_none() should remove the unused placeholders (placeholder example: [firstname]).
The author's email address is no longer working so I'm stuck. Anyone strong in regular expressions explain to me why it's not working?
/**
* Read and process HTML files
*
* @autor Daniel Chaves <irond@allmasters.com.br>;
* @version 1.0
* @access public
* @package IS Layout
*/
class IS_Layout
{
/**
* Contains the html´s souce code
* @var string
* @access private
*/
VAR $_code;
/**
* Informs if the lines are set
* @var bool
* @access private
*/
VAR $_linesSet=False;
/**
* IS_Layout Constructor
* @param string $arg path to html file or the html lines
* @access public
*/
function IS_Layout($arg=NULL)
{
if(isset($arg) and is_file($arg))
return $this->openFile($arg);
elseif(isset($arg) and is_string($arg))
$this->setCode($arg);
}
/**
* Opens the HTML file
* @param string $file Path to HTML file
* @return mixed boolean true, if successful or error message (see const SHOW_ERRORS)
* @access public
*/
function openFile($file)
{
$res=@fopen($file,"r");
if(!is_resource($res)){
return $this->_error('Fail to open file "'.$file.'"');
}
/**
* Sets the HTML lines
* @param string $str Lines of current HTML code
* @access public
*/
function setCode($str)
{
$this->_code=$str;
$this->_linesSet=True;
}
/**
* Replace variables on HTML code
* @param string $sId Identification of element
* @param mixed $items String or Array of values
* @access public
*/
function replace($sId, $items)
{
if(is_array($items)){
foreach($items as $k=>$v){
if(is_array($v)){
foreach($v as $kb=>$v){
$this->_doReplace("$sId,$k,$kb",$v);
}
}else{
$this->_doReplace("$sId,$k",$v);
}
}
}else{
$this->_doReplace($sId,$items);
}
}
/**
* Do a loop in part of HTML code replacing variables
* @param string $Id Identification of element
* @param array $Values Array of values
* @access public
*/
function loop_replace($Id, $Values)
{
$arReturn=$this->_getElement('replace',"id=\"$Id\"");
$sReplaceLines=$arReturn[0];
foreach($Values as $aValue){
$sTempValue=$sReplaceLines;
foreach($aValue as $key=>$value){
$sTempValue=ereg_replace("\[$key\]",(string)$value,$sTempValue);
}
$sReturn.=$sTempValue;
}
/**
* Get an Element of HTML Code
* @param string $Name Identification of element
* @param mixed $args String or Array of values passed in identification
* @access private
*/
function _getElement($Name, $args=NULL)
{
if(is_array($args)){
$comp="\s{1,}";
foreach($args as $key=>$value)
$comp.="$key=\"$value\"\s{0,}";
}elseif($args!=NULL){
$comp="\s{0,}".$args;
$comp=preg_replace("[\s{1,}]","\s{1,}",$comp);
}
$preg="|<!-- $Name$comp(.*?)-->(.*?)<!-- end $Name -->|s";
/**
* Replace an Element of HTML Code
* @param string $Name Identification of element
* @param mixed $args String or Array of values passed in identification
* @param string $el HTML code for substitute the code between the element
* @access private
*/
function _Element_replace($Name, $args=NULL, $el=NULL)
{
if(is_array($args)){
$comp="\s{1,}";
foreach($args as $key=>$value)
$comp.="$key=\"$value\"\s{0,}";
}elseif($args!=NULL){
$comp="\s{0,}".$args;
$comp=preg_replace("[\s{1,}]","\s{1,}",$comp);
}
$preg="|<!-- $Name$comp(.*?)-->(.*?)<!-- end $Name -->|s";
$this->_code=preg_replace($preg,$el,$this->_code);
}
/**
* Get part of main HTML code
* @param string $Id Identification of element
* @return object Object contains the main code
* @access public
*/
function getCode($Id)
{
$code=$this->_getElement('code', "id=\"$Id\"");
return new IS_Layout($code[0]);
}
/**
* Remove part of main HTML code
* @param string $Id Identification of element
* @param string $Code HTML code to replace
* @access public
*/
function code_replace($Id,$Code)
{
$this->_Element_replace('code', "id=\"$Id\"",$Code);
}
/**
* Remove part of main HTML code
* @param string $Id Identification of element
* @access public
*/
function remove($Id)
{
$this->_Element_replace('remove', "id=\"$Id\"");
}
/**
* Include code to main HTML
* @param object IS_Layout $Inc Object of layout
* @access public
*/
function inc($Inc)
{
$this->replace('include', $Inc->toHtml());
}
/**
* Replace a variable in HTML code
* @param string $Id Identification of element
* @param string $Item code
* @access private
*/
function _doReplace($Id, $Item)
{
$this->_code=ereg_replace(quotemeta("[$Id]"), (string)$Item, $this->_code);
}
/**
* Replace non used elements
* @access private
*/
function _replace_none()
{
$this->_code=ereg_replace("\\[([^\\[]*),([^\\[]*)\\]",'',$this->_code);
$this->_code=ereg_replace("\\[([^\\[]*),([^\\[]*),([^\\[]*)\\]",'',$this->_code);
$this->_code=preg_replace("|<!-- code(.*?)-->|s",$el,$this->_code);
$this->_code=preg_replace("|<!-- end code -->|s",$el,$this->_code);
$this->_Element_replace('replace');
}
/**
* Return HTML code
* @return string $Code Code HTML
* @access public
*/
function toHtml()
{
$this->_replace_none();
return $this->_code;
}
/**
* Print HTML code
* @access public
*/
function display()
{
if(!$this->_linesSet)
return False;
Bookmarks