Jump to content

[SOLVED] php template


B34ST

Recommended Posts

Hi, recently I have been trying to write a template class and found a great start on codewalkers.com

 

I can replace tags but the problem is when i am trying to replace tags in a loop, here is what I have :

 

snippet form template class:

function get_block($block) {
	preg_match ('#<!-- START '. $block . ' -->([^*]+)<!-- END '. $block . ' -->#',$this->page,$this->return);
	$code = str_replace ('<!-- START '. $block . ' -->', "", $this->return[0]);
	$code = str_replace ('<!-- END '  . $block . ' -->', "", $code);
	return $code;
}

function replace_block_tags($blockname, $data) {
	$blockCode = $this->get_block($blockname);
	foreach ($data as $key => $value) {
		$blockCode = str_replace ("{".$blockname.".".$key."}", $value, $blockCode);
	}

	$this->page = str_replace ($this->return[0], $blockCode.$this->return[0], $this->page);
} 

 

index.php:

$sql = "SELECT * FROM users";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
$page->replace_block_tags("users_block",array(
                         "user_id" => $row['user_id'],
		 "username" => $row['username'],
		 "email" => $row['email']));
}

 

index.tpl:

<table>
<!-- START users_block -->
<tr>
  <td>{users_block.user_id}</td>
  <td>{users_block.username}</td>
  <td>{users_block.email}</td>
</tr>
<!-- END users_block -->
</table>

 

This works fine however it always adds an extra row for example:

1

user 1

email 1

2

user 2

email 2

{users_block.user_id}

{users_block.username}

{users_block.email}

 

how can I remove or hide that last row?

 

thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/150200-solved-php-template/
Share on other sites

In the past I have tried smarty and other template engines in the past however although smarty is a great templating sytem I just find it too large for my needs. If i can fix the above problem I will have a templating system which suits me perfectly and is only 40 lines of code.

Link to comment
https://forums.phpfreaks.com/topic/150200-solved-php-template/#findComment-788792
Share on other sites

well...your code finds the block, inserts the values, then replaces the original block with the new block and the original block (so there is something there for the next data entry)

 

try writing a function called block_done($block) which finds the block and replaces it with nothing:

   function block_done($blockname) {
     $blockCode = $this->get_block($blockname);
     $this->page = str_replace($blockCode,'',$this->page);
  } 

 

and then run that after your while() loop

Link to comment
https://forums.phpfreaks.com/topic/150200-solved-php-template/#findComment-788812
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.