Code: Select all
$x = eval("return \"$x\";");template.txt
Code: Select all
My name is $name. Hello.Code: Select all
$x = file_get_contents("template.txt");
$name = "Chris";
$x = eval("return \"$x\";");
echo $x;Moderator: General Moderators
Code: Select all
$x = eval("return \"$x\";");Code: Select all
My name is $name. Hello.Code: Select all
$x = file_get_contents("template.txt");
$name = "Chris";
$x = eval("return \"$x\";");
echo $x;Code: Select all
$name = 'Chris';
include 'template.txt';Code: Select all
<?php
//-#################################################################
//-## Name: Simple Template System
//-## About: Provides an easy way of printing data from a html file.
//-## Author: Afterlife_69 (Dean Newman) http://www.gamerzvault.com
//-#################################################################
class template
{
var $tpldir = '';
var $var_array = array();
function sethome($file_dir)
{
$this->tpldir = $file_dir;
}
function clear_vars()
{
unset($this->var_array);
$this->var_array = array();
}
function unset_var($varname)
{
unset($this->var_array[$varname]);
}
function send_var($name, $data)
{
$this->var_array[$name] = $data;
}
function send_vars($data_arr)
{
if( ! empty($data_arr) && @is_array($data_arr) )
{
foreach( $data_arr AS $name => $data )
{
$this->var_array[$name] = $data;
}
}
}
function send_block_vars($blockName, $data_arr)
{
foreach($this->blk_array AS $Name => $data)
{
if($Name == $blockName)
{
$this->blk_array[$blockName]['Count']++;
}
}
if( is_array($data_arr) )
{
foreach($data_arr AS $item => $data)
{
$blockData[$blockName . '.' . $item] = $data;
}
}
$this->blk_array[$blockName]['DATA'] = $blockData;
}
function parse($file)
{
if( empty($this->tpldir) )
{
die('<b>Template Error:</b> No Template directory was set.');
}
if(! $file = @file_get_contents($this->tpldir . $file) )
{
die('<b>Template Error:</b> The requested file (' . $file . ') does not exist.');
}
if( empty($file) )
{
die('<b>Template Error:</b> The requested file (' . $file . ') was empty for handle.');
}
$lines = explode("\n", $file);
if(! empty($this->var_array) )
{
foreach($this->var_array AS $var => $data)
{
foreach($lines AS $line)
{
$lines = str_replace('{' . $var . '}', $data, $lines);
}
}
}
foreach($lines AS $line)
{
$line = preg_replace('#\{(.*?)}#is', 'undefined', $line);
echo $line . "\n";
unset($line);
}
unset($lines);
}
}
//-#################################################################
//-## Name: Simple Template System
//-## About: Provides an easy way of printing data from a html file.
//-## Author: Afterlife_69 (Dean Newman) http://www.gamerzvault.com
//-#################################################################
?>Code: Select all
<?php
include('./template.php');
$template = new template();
$template->sethome('./tpl/');
$template->send_var('DATA', "$var OR Data!'");
$template->send_vars(array(
'DATA1' => 'DATA: 1',
'DATA2' => 'DATA: 2',
'DATA3' => 'DATA: 3',
));
$template->send_block_vars('myblock', array(
'DATA' => '$data?',
));
$template->parse('file.tpl');
$template->clear_vars();
?>Code: Select all
{DATA1}<br />{DATA2}<br />{DATA3}<br />{DATA}Now do that with a string you've parsed from an XML file as I was doing this morning.sweatje wrote:It would probably be faster and less dangerous to do:and if you actually need the info as a string, use output buffering.Code: Select all
$name = 'Chris'; include 'template.txt';
Unless you expect the XML file to change every request, you are probably better off restructuring you application by writing the string out to an includable file and changing you XML parser to only fire if you cache is invalid.onion2k wrote: Now do that with a string you've parsed from an XML file as I was doing this morning.
onion2k wrote:Now do that with a string you've parsed from an XML file as I was doing this morning.sweatje wrote:It would probably be faster and less dangerous to do:and if you actually need the info as a string, use output buffering.Code: Select all
$name = 'Chris'; include 'template.txt';
Code: Select all
$name = 'Bruce';
include 'var://parsed_xml';Yep I've seen it done with a php:// protocolWeirdan wrote:, provided that var:// stream wrapper is registeredCode: Select all
$name = 'Bruce'; include 'var://parsed_xml';