POG1 Posted December 23, 2008 Posted December 23, 2008 With some websites i have noticed that you can create templates and i thought it would be good to have it with mccodes. The only problem is i don't know how to do it, the first idea was to get the template file contents and split it up. Somethng like this. with each section in the test.tpl having {NEXT-SECTION} to mark the end of the section. <?php $template = "test.tpl"; $contents = file_get_contents($template); $page = explode("{NEXT-SECTION}",$contents); class page { function headers() { global $page; echo $page[0]; } function menuArea() { global $page; echo $page[1]; } function end() { global $page; echo $page[2]; } } $p = new page; $p->headers(); $p->menuArea(); echo 'PAGE CONTENT'; $p->end(); ?> I also found somewhere something very simple, but it dosent work :( Could someone give some ideas as to what would be the best way to do this.. <?php $file = "test.tpl"; $contents = file_get_contents($file); $website_tpl = 'something'; $text = preg_replace('/\{(\w+)\}/e', '$$1_tpl', $TEMPLATE); echo $text ?> Quote
radio_active Posted January 1, 2009 Posted January 1, 2009 Re: template engine What are mccodes? Crap Quote
shaved92bravada Posted January 1, 2009 Posted January 1, 2009 Re: template engine What are mccodes? :roll: Quote
AlabamaHit Posted January 1, 2009 Posted January 1, 2009 Re: template engine im not sure what you mean? do you mean where pages are displayed for exapmle index.php?page=bank or index.php?page=explore ? Quote
Eldorik Posted January 1, 2009 Posted January 1, 2009 Re: template engine What are they? I've never heard of them. Do you mean splitting your page up? Or including other templates from a template file? Please explain. Quote
Karlos Posted January 1, 2009 Posted January 1, 2009 Re: template engine MCCodes... Game Engine... Bullcrap... PHP & MySQL... No Security... Stay Away From It... :wink: Quote
Karlos Posted January 1, 2009 Posted January 1, 2009 Re: template engine Fair enough. :lol: +1 gave me a laugh Quote
POG1 Posted January 1, 2009 Author Posted January 1, 2009 Re: template engine does anyone know of a ready made template engine that you can just create a file and it will put it on the page. Quote
AlabamaHit Posted January 1, 2009 Posted January 1, 2009 Re: template engine im not sure what you mean? do you mean where pages are displayed for exapmle index.php?page=bank or index.php?page=explore ? is that what you mean? a template on every page man..that don't exlain much, lol..cause that could be as simple as making a function to include a page and call the function..... Explain what you are trying to get to...I will try my best to help ya out....... Quote
POG1 Posted January 1, 2009 Author Posted January 1, 2009 Re: template engine with website packages such as wordpress you can select templates and save the file in its own dir. I want to use that with games. Quote
mdshare Posted January 2, 2009 Posted January 2, 2009 Re: template engine or just create your own I had no need for smarty as it was just minor template changes here is a example function used on cetwg with file_get_contents() template related function template($title,$meta_description,$meta_keywords,$meta_robots,$main_content){ function template($title,$meta_description,$meta_keywords,$meta_robots,$main_content){ if (!$title){ $title = 'CE TopWebGames'; } if (!$meta_description){ $meta_description = 'description'; } if (!$meta_keywords){ $meta_keywords = $site_name.', blah, blah2, blah3, ...blah30'; } if (!$meta_robots){ $meta_robots = 'index, follow'; } $main_content .= ''; echo str_replace(array("[+]title[+]", "[+]meta_description[+]", "[+]meta_keywords[+]", "[+]meta_robots[+]", "[+]main_content[+]"), array($title, $meta_description, $meta_keywords, $meta_robots, $main_content), file_get_contents('headerandfooter.txt')); } so main_content. = is basicly your echo for the main content of your template title, meta stuff becomes dynamic as you asign for each page a new value, yet you use the same template file in my case simply headerandfooter.txt so at my tx file ... ... <div id="maincontent"> [+]main_content[+] </div> ... [ + ]main_content[ + ] gets nicely replaced by all that has to be echo'd Quote
shedh Posted March 12, 2009 Posted March 12, 2009 Re: template engine i have modified the code above to what i want it to do, but i am having some problems, this is what my code looks like: <span class="syntaxdefault"><?php </span><span class="syntaxkeyword">function </span><span class="syntaxdefault">template</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$page</span><span class="syntaxkeyword">){ include(</span><span class="syntaxstring">"./mods/$page.php"</span><span class="syntaxkeyword">); if (!</span><span class="syntaxdefault">$title</span><span class="syntaxkeyword">){ </span><span class="syntaxdefault">$title </span><span class="syntaxkeyword">= </span><span class="syntaxstring">'CrimsonVille'</span><span class="syntaxkeyword">; } &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; if(!</span><span class="syntaxdefault">$page</span><span class="syntaxkeyword">){ &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; </span><span class="syntaxdefault">$page </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"index"</span><span class="syntaxkeyword">; &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; &</span><span class="syntaxdefault">nbsp</span><span class="syntaxkeyword">; } </span><span class="syntaxdefault">$content </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">file_get_contents</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"mods/" </span><span class="syntaxkeyword">. </span><span class="syntaxdefault">$page </span><span class="syntaxkeyword">. </span><span class="syntaxstring">".php"</span><span class="syntaxkeyword">); echo </span><span class="syntaxdefault">str_replace</span><span class="syntaxkeyword">(array(</span><span class="syntaxstring">"[+]title[+]"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"[+]main_content[+]"</span><span class="syntaxkeyword">), array(</span><span class="syntaxdefault">$title</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">$content</span><span class="syntaxkeyword">), </span><span class="syntaxdefault">file_get_contents</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'templates/default/header.html'</span><span class="syntaxkeyword">)); } </span><span class="syntaxdefault">template</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"index"</span><span class="syntaxkeyword">); </span><span class="syntaxdefault">?> </span> what it is doing is that it echo's the content required before the "<!DOCTYPE " tag, and only shows on the output page if i don't use php codes like echo"blah"; can anyone help? Quote
POG1 Posted March 12, 2009 Author Posted March 12, 2009 Re: template engine @shedh. Smarty will do all the work for you, all you will need to do is declare the class and build the page. include("./mods/$page.php"); That could be potentially dangerous. Check out these functions that could help you from getting attackes. Isset Ctype Alnum File Exists Quote
Floydian Posted March 12, 2009 Posted March 12, 2009 Re: template engine That include isn't dangerous if he's always passing a string that's hard coded in the script, which is what he's doing in that example. Quote
POG1 Posted March 12, 2009 Author Posted March 12, 2009 Re: template engine Yeah, but he could probably be using the parameter with a GET variable. Quote
Haunted Dawg Posted March 12, 2009 Posted March 12, 2009 Re: template engine $page = (file_exists($page) && ctype_alnum($page)) ? $page : FALSE; if(!$page) { echo 'Invalid Page.'; } Quote
shedh Posted March 12, 2009 Posted March 12, 2009 Re: template engine right on another page i have this: <span class="syntaxdefault"> $title </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"Page Title"</span><span class="syntaxkeyword">; echo</span><span class="syntaxstring">"lorem ipsum, blah blah blah"</span><span class="syntaxkeyword">; </span><span class="syntaxdefault"></span> it replaces [+]title[+] with "Page title", but it still outputs "lorem ipsum, blah blah blah" before the "<!DOCTYPE" Quote
POG1 Posted March 12, 2009 Author Posted March 12, 2009 Re: template engine Try something like.. $main_content = 'lorem ipsum.'; Quote
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.