Jump to content
MakeWebGames

Making a small templating system (need feedback)


Sim
 Share

Recommended Posts

Heres my template class:

 

<?

class template
{
private $template;
private $Replace;
private $Replacer;

public function __construct($template_dir, $page)
{
	$this->template = file_get_contents("templates/" . $template_dir . "/index.htm");
	$this->template = str_replace("{CONTENT}", file_get_contents("templates/" . $template_dir . "/$page.htm"), $this->template);
}

//this function will set vars to be replaced
public function set_var($word, $replace)
{
	$this->Replace[] = $word;
	$this->Replacer[] = $replace;

}

//proccess all vars that needs to be replaced
public function proccess()
{	
	//replace all vars needed to be replaced
	for($x=0; $x<count($this->Replace); $x++)
	{
		$this->template = str_replace("{" . $this->Replace[$x] . "}", $this->Replacer[$x], $this->template);
	}
}

public function display()
{

	//template colors (TODO: replace colors with SQL info)
	$Replaces = array("background", "link_color", "header", "user_info_block", "right_sidebar", "left_sidebar", "content", "footer", "titlebar", "shadows",
	"test_color", "input_text", "input_background");
	$Replacers = array("#fff", "#fff", "#007AC4", "#035883", "#007AC4", "#007AC4", "#007AC4", "#007AC4", "#C24464", "#333", "#fff", "#444", "#fff");


	//set all colors in template
	for($x=0; $x<count($Replaces); $x++)
	{
		$this->template = str_replace("{" . $Replaces[$x] . "}", $Replacers[$x], $this->template);
	}

	//replace all vars
	$this->proccess();

	//display template
	echo $this->template;
}
}

 

Here is how a example of index page:

 


include "helpers/template.php";
$template = new template("default/admin", "main");

$template->set_var("game_name", "Testing Template var check");
$template->set_var("game_url", "testing.com");
$template->display();
?>

 

It works sorta like this:

You create your ONE HTML file that is the main page which has your header and footer menu's and all that stuff that is always static. Then where you want your content to be displayed, it has {CONTENT} which loads the other page and replaces the {CONTENT}

I got a few more thing's that needs to be done. Such as looping data for template and set array vars instead of vars one at a time. Whats everyone think. Anyone got any feedback.

Link to comment
Share on other sites

Actually I would go for pure PHP in most cases. Why? because PHP iteself can be used as template. Why replace <?= $content ?> with {CONTENT} ? save a couple of characters? I hardly see the need for that.

However I'm all in favor of separation of the look and the logic.

Link to comment
Share on other sites

A lot of templating systems are overkill. Smarty for example has a ton of files...

I don't know about twig, never used it.

There are template systems for each's need.

Lightweight: RainTPL

Popular/Buzzy: Smarty

Performance: Twig

RaintTPL for instance, is a bit over 1k lines and one file, and offers syntax comparable to Twig/Smary, and also has a changeache

Link to comment
Share on other sites

I quite like the idea of doing it yourself, a sense of achievement. If you manage to do it all, and it works, as intended, great! If not, well, you tried, and learned, I bet!

Anyway, I can see it being cool, and such, but I read somewhere about short tags not being supported in some browsers, or whatever, so, to be on the safe side, edit those.

Link to comment
Share on other sites

I quite like the idea of doing it yourself, a sense of achievement. If you manage to do it all, and it works, as intended, great! If not, well, you tried, and learned, I bet!

Anyway, I can see it being cool, and such, but I read somewhere about short tags not being supported in some browsers, or whatever, so, to be on the safe side, edit those.

I think you meant PHP installations, and that's only because the ini directive is set to have it disabled.

Since PHP 5.4 it's always enabled.

Link to comment
Share on other sites

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.

 Share

×
×
  • Create New...