ultrus Posted June 30, 2008 Share Posted June 30, 2008 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. Link to comment https://forums.phpfreaks.com/topic/112599-solved-help-with-seperating-the-code-from-the-design/ Share on other sites More sharing options...
ultrus Posted June 30, 2008 Author Share Posted June 30, 2008 So far I'm finding this link useful: http://www.developertutorials.com/tutorials/php/php-template-script-050316/page1.html Link to comment https://forums.phpfreaks.com/topic/112599-solved-help-with-seperating-the-code-from-the-design/#findComment-578269 Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 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. Link to comment https://forums.phpfreaks.com/topic/112599-solved-help-with-seperating-the-code-from-the-design/#findComment-578281 Share on other sites More sharing options...
rhodesa Posted June 30, 2008 Share Posted June 30, 2008 also, check out Smarty: http://www.smarty.net/ it might be a little more then you want, but it works well Link to comment https://forums.phpfreaks.com/topic/112599-solved-help-with-seperating-the-code-from-the-design/#findComment-578288 Share on other sites More sharing options...
ultrus Posted July 1, 2008 Author Share Posted July 1, 2008 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; } ?> Link to comment https://forums.phpfreaks.com/topic/112599-solved-help-with-seperating-the-code-from-the-design/#findComment-579083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.