note 83782 modified in function.eval by danbrown
| From: | danbrown@php.net | Date: | Tue, 25 Nov 2008 16:02:38 +0000 |
| Subject: | note 83782 modified in function.eval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146934@lists.php.net to get a copy of this message | ||
The EVAL function can be used as a fast Template system.
<?php
function parseTemplate($template, $params=array()) {
foreach ($params as $k=>$v) {
$$k = $v;
}
ob_start();
eval("?>" . implode("", file($template)) . "<?");
$c = ob_get_contents();
ob_end_flush();
return $c;
}
?>
Example:
<?php
echo parseTemplate("myTemplate.php", array('account'=>$row));
?>
and myTemplate.php can be like
<?php foreach($account as $k=>$v) : ?>
<?php echo $k; ?>: <?php echo $v; ?>
<?php endforeach; ?>
--was--
The EVAL function can be used as a fast Template system.
<?
function parseTemplate($template, $params=array()) {
foreach ($params as $k=>$v) {
$$k = $v;
}
ob_start();
eval("?>" . implode("", file($template)) . "<?");
$c = ob_get_contents();
ob_end_flush();
return $c;
}
?>
Example:
<?
echo parseTemplate("myTemplate.php", array('account'=>$row));
?>
and myTemplate.php can be like
<? foreach($account as $k=>$v) : ?>
<?=$k?>: <?=$v?>
<? endforeach; ?>
http://php.net/manual/en/function.eval.php