how whould you do it?

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
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

how whould you do it?

Post by CONFIQ »

let's say...
you have file> html.tpl and in it

Code: Select all

<table $var $var2></table>
how whould you echo this HTML part if it is in while and that all variables whould be loaded.
'Till now i've figure something like this
index.php:

Code: Select all

<?php
for($i=1,$i>10,$i++){
$var=$i+1;
$var2=$i-1;
include("html.tpl");
}
?>
but it dosen't seems "handsome" to me... and i don't like include in this case...
is there a better way to do it?
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

The PHP function eval() parses a string as PHP code.
see:
http://www.php.net/manual/en/function.eval.php
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

Post by CONFIQ »

Hmmm, something like

Code: Select all

<?php
$string = implode('', @file('html.tpl'));
for($i=1,$i>10,$i++){ 
$var=$i+1; 
$var2=$i-1; 
eval ('?>' . $string);
} 
?>
   
good idea,thank you :)
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Easy'st:

Code: Select all

<table <? echo $var.$var2 ?>></table>
In your tempate.

The best is to use a existing template engine.
I use Yapter.
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Often, the purpose of using templates is to let design matters go into files where no PHP goes. None of the examples above provide this except for Yapter, which seems to be a nice template engine. I haven't tried it myself, but I read about it on http://www.yapter.com
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

Post by CONFIQ »

it remainds me on smarty.php.net ...
Anyway i can't use any template, i need something simple like this :)
Post Reply