Jump to content
MakeWebGames

template engine


Recommended Posts

Posted

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

?>

  • 2 weeks later...
Posted

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.

Posted

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.......

Posted

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.

Posted

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

  • 2 months later...
Posted

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?

Posted

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

Posted

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"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...