PDA

View Full Version : [PHP] Templating Engine Helppp ;[



MrCraig
04-09-2008, 04:02 PM
Ok, basically this is the first time ive EVER made a templating engine, and as a result, having a few problems with it.

The commented lines are just the things ive taken out so i can test it without database access.

template.inc.php


<?php
#require("dbconfig.php");

#$fetch = mysql_fetch_array(mysql_query("select * from `dpan_config` where `asp` = 'template'"));
#$template = $fetch["pref"];
$template = "default";

if(file_exists("skins/".$template."/login.htm"))
$login_template = "skins/". $template ."/login.htm";
elseif(file_exists("skins/".$template."/login.html"))
$login_template = "skins/". $template ."/login.html";
elseif(file_exists("skins/".$template."/login.php"))
$login_template = "skins/". $template ."/login.php";
else
die("INVALID TEMPLATE SPECIFIED.");

if(file_exists("skins/".$template."/main.htm"))
$main_template = "skins/". $template ."/main.htm";
elseif(file_exists("skins/".$template."/main.html"))
$main_template = "skins/". $template ."/main.html";
elseif(file_exists("skins/".$template."/main.php"))
$main_template = "skins/". $template ."/main.php";
else
die("INVALID TEMPLATE SPECIFIED.");

function compiletemplate($type)
{
if($type == "login")
{
$getfile = file_get_contents($login_template);

#$panel_message = mysql_fetch_array(mysql_query("select * from `dpan_config` where `asp` = 'panel_message'"));
#$panel_message = $panel_message["pref"];
$panel_message = "Heyyy";
$getfile = str_replace("{panel_message}",$panel_message,$getfile);

$logincode = '<form action="?login=login" method="post">
<div align="center">
Username:<br />
<input type="text" name="username" /><br /><br />
Password:<br />
<input type="password" name="password" /><br /><br />
<input type="submit" value="Login" />
</div>
</form>';
$getfile = str_replace("{logincode}",$logincode,$getfile);

echo($getfile);
}
elseif($type == "main")
{
$getfile = file_get_contents($main_template);

}
else
die("NO TEMPLATE TYPE SPECIFIED.");
}

?>
index.php


<?php
require("inc/template.inc.php");
compiletemplate('login');
?>
(Il add html tags etc to it later, was just checking it worked before i continued any further with it)


Error is that the page is not showing at all.
(Like a blank page)

Thanks for any help anyone may have. :)!

Blob
04-09-2008, 04:29 PM
<?php
class template
{
private $file, $template, $page;
public function parse( $template, $file, $page )
{
$this->template = $template; $this->file = $file; $this->page = $page;
$template = "templates/" . $template . "/" . $file . ".tpl";
if( !file_exists($template) )
{
exit ( "Cant find template file");
} else
{
$template = file_get_contents( $template );
$template = $this->custom_vars( $template );
return $template;
}
}
private function custom_vars( $template )
{
$matches = array();
if( preg_match_all( "#\\{([A-Za-z_]+)\\}#ie", $template, $matches ) )
{
for( $i = 0; $i < count($matches) - 1; $i++ )
{
$template = preg_replace('#\\{([A-Za-z_]+)\\}#ie', '$this->get_var("\\1")', $template);
}
}
return $template;
}
private function get_var( $var )
{
$template = "templates/" . $this->template . "/" . $var . ".tpl";
if( !file_exists($template) )
{
$core->user = new user;
if( $var == "TITLE" )
{
$return = $this->status( $this->page );
} elseif( $var == "CONTENT" )
{
$template = "templates/" . $this->template . "/pages/" . $this->page . ".tpl";
if( file_exists($template) )
{
$return = file_get_contents($template);
}
} elseif( $var == "PAGE_TITLE" )
{
if( $core->user->check_login() === true )
{
if( $this->page == "loggedin" )
{
$page = "main";
} else {
$page = $this->page;
}
} else
{
$page = $this->page;
}
$return = "hi";
} elseif( $var == "HEADER" )
{
if( $core->user->check_login() === true )
{
$return = "Welcome to BLANK, <strong>" . $_SESSION["username"] . "</strong>, you last logged in at " . $core->user->lastonline();
} else
{
$return = "Oh noes! Your not logged in!";
}
}
} else
{
$return = file_get_contents( $template );
}
return $return;
}
}
?>

tbh they should submit their own template to be parsed within etc

MrCraig
04-09-2008, 06:33 PM
Umm thanks ryan but i really only want to know whats wrong with existing code..

MrCraig
05-09-2008, 02:00 PM
NVM, got it working :)

Want to hide these adverts? Register an account for free!