PC Nerd Posted June 29, 2008 Share Posted June 29, 2008 Hi, Ive currently got a file that displays the page exactly how I want it, essentially a template. In it, wherever there are *unique* features, such as the page name, ive got: "[PAGE_NAME]". Im looking at different way sof diaplaying this on a large scale ( im building a CMS). as far as i know of ive got 4 options: 1. break apart the file into a range of smaller includes, and then build each page around that. 2. read that file and then use str_replace() to replace the unique parts 3. regular expressions???? ( not sure if they can replace text though). 4. Use a $PAGE array created elsewhere ( eg, in the CMS).... and have the template.php call the $PAGE values..... and then ocne proccessing etc etc is done, it simply goes: require(".../themes/template.php"); See where ive got the body of the page, its "[CONTENT]". I dont particularly want to have to use options one, because its harder to maintain ( i think). option 4 is ok, but what about an entire page's content? do you think thats getting unreasonable? Can anyone here offer a suggestion about how i shoudl be displaying this page? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted June 29, 2008 Share Posted June 29, 2008 If you really want to use a template engne of some sort, its probably best to just use a solution thats already developed. Something liek smarty maybe? Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted June 29, 2008 Author Share Posted June 29, 2008 hmmm.. I dont particularly want to be using any prebuilt engines or setups ( otherwise id just use joomla or moodle etc). msn messaging to friends has said that option 2 is best, but that it woudl require custom error reporting ... so writing my own version of str_replace(); say i have template.php ( although my directory structure is a lot more complecated than that). If i was to go to login.php eg [domain]/login.php - i would want it to display using template.php in template.php, instead of having [CONTENT], would it be better to have: <?php require("login.php");?> ? See the only problem with this is that a) my urls would all be displayed as [domain]/template.php, whichi i dont want. also - my directory structures are ued to build teh breadcrumbs menu, so that would be likely harder, but whats your opinion? Is there a way that i can code it this way, but fool php into thinking that login.php is the actual running script? thanks Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted June 29, 2008 Author Share Posted June 29, 2008 hmmmmm I thought id look at output buffering , because i know it captures output ( eg from echo etc)... and waiets for you to "flush" it beore it sdisplays...... I think thats what Ill do..... http://au.php.net/manual/en/function.ob-get-contents.php So ill program all my database stuff, and output.... something like: <?php start output buffering normal work require(template); output buffer then simply: </body> </hmtl> or whatever is needed?> I could also output body to a variable, and output menus to variables or use a combination of above.. .so what do you think of this? Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 I have written a similar system to this for an eCommerce package I wrote. I will try and explain how it works and maybe you could modify to work with you. I have a template system like yours with tags such as [PRODUCT-NAME] and [PRODUCT-ID] in it. I wrote a function that would take a query as it's argument, perform that query and for each column returned it would capitilize the column name, wrap square brackets around it and then attempt to replace any occurences of that with the value held in that column. For example: str_replace("[COLUMN-NAME]", $column_value, $template) The good side to doing it this way was that it wouldn't require loads of str_replace's as it is all done through iteration. Infact here is the function. The other function, insertVars() is used to insert generic variables that can't be included in the query. function insertfieldsRS($buffer, $query) { if ($buffer && $query) { $rs = mysql_query($query); if (countRS() > 0) { $fields = mysql_num_fields($rs); for($i = 0; $i < $fields; $i++) $buffer = str_replace("[".strtoupper(mysql_field_name($rs, $i))."]", output(mysql_result($rs, 0, $i)), $buffer); $buffer = insertVars($buffer); return $buffer; } } } function insertVars($buffer) { $buffer = str_replace("[ROOT]", sRoot(), $buffer); $buffer = str_replace("[sITE-NAME]", _fullName, $buffer); $buffer = str_replace("[MAIN-COLOUR]", _mainColour, $buffer); $buffer = str_replace("[sECURE-ROOT]", secureRoot(), $buffer); $buffer = str_replace("[sTORE-ROOT]", pRoot(), $buffer); $buffer = str_replace("[ROOT]", sRoot(), $buffer); return $buffer; } I'm not whether or not this is what you're looking for but after working with templates for years, this system has finally made things a lot easier and more efficient for us! Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted June 29, 2008 Author Share Posted June 29, 2008 ive got the following working 100%: <?php $OUTPUT = array(); $PAGE_LEVEL = 2; $_USER['doc_root'] = '../'; ob_start(); ### ### Main page output, start template buffering ### echo "Content part 1<br>"; echo "Content part 2<br>"; echo "Content part 2<br>"; $OUTPUT['CONTENT'] = ob_get_contents(); #$OUTPUT['CONTENT'] = "TESTING!!!"; #ob_get_flush(); ob_clean(); #ob_start(); require("../themes/template.php"); $OUTPUT['template'] = ob_get_contents(); ob_clean(); #ob_start(); require("../pre/crumbs.php"); $OUTPUT['CRUMBS'] = ob_get_contents(); ob_clean(); #echo $OUTPUT['template']."<br>"; #echo $OUTPUT['CRUMBS']."<br>"; $OUT = ""; #$OUT .= $OUTPUT['CRUMBS']; #$OUT .= "<br>".$OUTPUT['template']; #$OUT .= "<br>".$OUTPUT['CONTENT']; $OUTPUT['template'] = str_replace("[%CONTENT%]", $OUTPUT['CONTENT'], $OUTPUT['template']); $OUTPUT['template'] = str_replace("[%CRUMBS%]", $OUTPUT['CRUMBS'], $OUTPUT['template']); echo $OUTPUT['template']; #echo "This is my page What do you think?<br><br>"; #echo $_SERVER['SCRIPT_NAME']; #echo "<br>"; #echo __FILE__; #cho "<br>"; #echo $_SERVER['SCRIPT_FILENAME']; ?> I know its reasonably messy - but you can see that the template has [%.....%] markers, and thers where i customise my page. ive made use of the $_SERVER['SCRIPT_NAME'] variable to build the breadcrumb trail. I havent finished building all of the menus etc - but the templating is working. let me know what you think. *** Thanks to al the suggestions here - this is what im going with. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.