Something Really Obvious

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Something Really Obvious

Post 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.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
Afterlife(69)
Spammer :|
Posts: 14
Joined: Mon Oct 03, 2005 4:51 am

Post 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}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply