As of right now I've created a template class, and I've created a registration class. But I'm having trouble getting the two to work properly together so that I can display my variables in my template files.
Here are the basics of my template class:
class siteTemplate {
function getTemplate($file, $varesc=false) {
if (file_exists("templates/" . $file)) {
$data = file_get_contents("templates/" . $file);
$data = str_replace("\"","\\\"", $data);
$data = str_replace("\'","\\\'", $data);
$data = str_replace("\\n","\\\n", $data);
if($varesc)
$data = str_replace("\$","$", $data);
return $data;
} else {
die("Error.<br />Could not find <strong>" . $file . "</strong>.");
}
}
function createGlobal() {
global $siteName, $siteUrl;
global $content;
eval("\$main = \"".$this->getTemplate("main.html")."\";");
echo $main;
}
}
$tp = new siteTemplate();
A function from my registration class:
public function get_username($uid) {
$result = mysql_query("SELECT username FROM users WHERE uid开发者_高级运维 = $uid");
$user_data = mysql_fetch_array($result);
echo $user_data['username'];
}
I can echo out data from my registration class in index.php
echo $user->get_username($uid);
BUT I can't do the same thing within my template files. What adjustments do I need to make to make this work together. Live example: http://www.aarongoff.com/i Username: test Password: test
If you look I'm echoing out "Logged in as: test" But when I try to call for that variable within my template file it just displays "Logged in as:"
(I know there are SQL vulnerabilities, I'm just testing to get my classes to work)
The true answer to this is that PHP IS a template! Use pure PHP code as your templates. Then you don't have to keep reimplementing every one of PHP's features in your ad hock template class.
This is called the http://en.wikipedia.org/wiki/Inner-platform_effect and you should avoid it. Just use PHP directly, it's what it was made for.
What you should do is be disciplined about naming the PHP files, and separating concepts logically. But don't try to reimplement PHP in PHP.
精彩评论