Page 1 of 1

Something Really Obvious

Posted: Mon Oct 03, 2005 4:35 am
by onion2k

Code: Select all

$x = eval("return \"$x\";");
If $x is a string loaded from a text file (or database, whatever) and has some variable names in it then this snippet will turn them into their values. For example:

template.txt

Code: Select all

My name is $name. Hello.
index.php

Code: Select all

$x = file_get_contents("template.txt");
$name = "Chris";
$x = eval("return \"$x\";");

echo $x;
Really, really obvious, but it's proven very useful this morning.

Posted: Mon Oct 03, 2005 7:22 am
by sweatje
It would probably be faster and less dangerous to do:

Code: Select all

$name = 'Chris';
include 'template.txt';
and if you actually need the info as a string, use output buffering.

Posted: Mon Oct 03, 2005 8:21 am
by Afterlife(69)
Meh. i made a template system, works a lil better :D

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
//-#################################################################
?>
test.php:

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();

?>
file.tpl

Code: Select all

{DATA1}<br />{DATA2}<br />{DATA3}<br />{DATA}

Posted: Mon Oct 03, 2005 9:10 am
by onion2k
sweatje wrote:It would probably be faster and less dangerous to do:

Code: Select all

$name = 'Chris';
include 'template.txt';
and if you actually need the info as a string, use output buffering.
Now do that with a string you've parsed from an XML file as I was doing this morning.

Posted: Mon Oct 03, 2005 9:27 am
by sweatje
onion2k wrote: Now do that with a string you've parsed from an XML file as I was doing this morning.
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.

Remember that eval takes an incredible hit to performance because there is no way for it to take advantage of a byte code cache.

Posted: Mon Oct 03, 2005 3:21 pm
by Weirdan
onion2k wrote:
sweatje wrote:It would probably be faster and less dangerous to do:

Code: Select all

$name = 'Chris';
include 'template.txt';
and if you actually need the info as a string, use output buffering.
Now do that with a string you've parsed from an XML file as I was doing this morning.

Code: Select all

$name = 'Bruce';
include 'var://parsed_xml';
, provided that var:// stream wrapper is registered :)

Posted: Mon Oct 03, 2005 5:58 pm
by Chris Corbyn
Weirdan wrote:

Code: Select all

$name = 'Bruce';
include 'var://parsed_xml';
, provided that var:// stream wrapper is registered :)
Yep I've seen it done with a php:// protocol :) No server needed ;) Still... if you want to do it purely with code, this does the trick.