Jump to content

[SOLVED] help with seperating the code from the design


ultrus

Recommended Posts

Howdy,

I'm working on a page where I need to display comments in a templated format. Generally, I design the template without functionality, then merge it into a function for easy use by the dev guys. This is great, ... until I change the overall design of the website. It would be helpful to include an external template to be populated as needed. Below is a way simplified example of what one of my functions would look like (imagine lots of added tables and comments, comments pulled from database in function, not specified in the function call itself):

 

<?php
function getComment($comment, $id) {
$htmlContent = '<div id="' . $id . '">';
$htmlContent .= $comment;
$htmlContent .= '</div>';
return $htmlContent;
}
?>

<?php echo getComment("This is just a demonstration.", 1); ?>

 

Here is a bad example of what I want instead. This won't work of course, but you get the idea:

 

Template file:

<div id="<?php echo $id; ?>">
<?php echo $comment; ?>
</div>

 

Function file file:

<?php
function getComment($comment, $id) {
$htmlContent = include templateFile.php; //could I do something like this? - this does not work obviously 
return $htmlContent;
}
?>

<?php echo getComment("This is just a demonstration.", 1); ?>

 

I'm somewhat aware of template based web tools like CakePHP, however I don't want to add any more than needed for this project. Any thoughts? I appriciate the feedback muchly. :D

 

 

I've written my own template system. You pass a template and a MySQL query to the database. The function takes each column, makes the column name uppercase, puts square brackets around it, then attempts to replace that in the template with the value in the column.

 

For example if I queried...

 

<?php

echo insertVars($template, "SELECT productID, productName FROM tblProduct WHERE id = 1");

?>

 

The following code would be run on the template...

 

<?php

$template = str_replace("[PRODUCTID]", mysql_result($rs, $row, "productID"), $template);
$template = str_replace("[PRODUCTNAME]", mysql_result($rs, $row, "productName"), $template);

?>

 

Using this technique you simply add more values to the query and then you can easily update the template.

 

I'm sorry but my explanation of this technique doesn't do it justice! If this is what you think you want let me know and I can post some of my functions.

Thanks for the tips! Smarty is pretty cool, too big for this project though. Something like the following is working great (referenced from link posted):

 

<?php
function makeFromTemplate($templateFile, $varArray) {
if(!$fd = fopen($templateFile, "r")) {
	$content = "ERROR: Template file not found.";
} else {
	$content = fread($fd, filesize($templateFile));
	fclose ($fd);
	$content = stripslashes($content);
	foreach($varArray as $var) {
		$content = str_replace($var[0], $var[1], $content); //$var[0] = "<[ placeholder ]>", $var[1] = "value"
	}
}	
return $content;
}

?>

 

:)

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.