PHP template system help

10/24/2014 22:03 mlukac89#1
Hi,
I'm working on some cms for gamers and teams, wars, etc...

But i have problem how to separate html from php.
For example i want make all includes so when u make template u just include file in and thats it
Code:
<?php include 'latest_news'; ?>
for example.

Now i have

index.php ( here will be all includes, css, js )
login.php ( here will be checks for user data )
FOLDER named Templates -> login.html

So now i need when i finish all in login.php to call data from html file and in html file just to be variables from login.php

like

Code:
<?php

if (!defined('ACCESS')) {
	die('Forbidden!');
}

if ($general->is_logged() === true) {

	include 'inc/logged.html';
	
} else {

// login form
if (isset($_POST['login']))
{
	$username = $_POST['username'];
	$password = $general->safepass($_POST['password']);
	$last_login_date = time();
	$ip = $general->get_ip();

	if (empty($username) || empty($password))
	{
		$error = $lang['login_error_user'];
	}
	else
	{
		$login = $users->login($username, $password);

		// cookie login
		if (isset($_POST['stay_logged']))
		{
			if ($login) 
			{
				$user_id = $login['id'];

				$expire = time()+60*60*24*30;

				$rand = hash('sha512', mt_rand());

				setcookie('token', $rand, $expire);
				setcookie('username', $login['username'], $expire);
				setcookie('id', $login['id'], $expire);

				$update_login_data = $users->update_cookie_login($last_login_date, $ip, $rand, $user_id);

				header('Location: index.php');
				exit();
			}
			else
			{
				$error = $lang['login_error_data'];
			}
		}

		// session login
		else
		{
			if ($login) 
			{
				$_SESSION['id'] = $login['id'];
				$_SESSION['username'] = $login['username'];
				$user_id = (int)$_SESSION['id'];

				$update_login = $users->update_user_ip_login($last_login_date, $ip, $user_id);

				header('Location: index.php');
				exit();
			}
			else
			{
				$error = $lang['login_error_data'];
			}
		}
	}
}

}

?>
Code:
<form action="" method="POST">
<input type="text" name="username" placeholder="<?php echo $user?>">
<input type="password" name="password" placeholder="<?php echo $pass?>">
<input type="submit" name="login" value="<?php echo $login_button?>">
</form>
10/24/2014 23:04 KoKsPfLaNzE#2
you have to write a tempalte engine for that or use smth like twig or smarty. btw the system with seperate html from php is a system like MVC, if you good in PHP you can use a framework, so you dont have to start at zero with the code.
10/24/2014 23:14 mlukac89#3
Nah i dont like that template engines and MVC too, but i saw 1 way in one cms itsno so hard its go like this

Code:
// this variables i can use in that template
$forgot_pass = $lang['login_error_forgot_pass'];
$register = $lang['login_error_register'];
$login_btn = $lang['login_error_button'];

// so this part calls template html and shows a form
eval ("\$login = \"".$general->gettemplate("login")."\";");
echo $login;
and this code i use in class

Code:
public function gettemplate($template, $ext='html', $calledfrom='root')
	{
		
		$templatefolder = 'inc';
		
		if ($calledfrom=='root')
		{
			return str_replace("\"", "\\\"", file_get_contents($templatefolder.'/'.$template.'.'.$ext));
		}
		elseif ($calledfrom=='admin')
		{
			return str_replace("\"", "\\\"", file_get_contents('../'.$templatefolder.'/'.$template.'.'.$ext));
		}
	}
but im not sure is there more simple solution and if this is safe one.