...

View Full Version : Website template with classes



Jacka
11-14-2007, 08:58 PM
I recently decided that my website needed a new way of being generated, and after a bit of research, I came to the conclusion that classes would be the best for this.

Unfortunately, I've come to a few dead ends, the first of which is adding menu items.

Here is my code so far.

Classes


<?php
class template{
private $template;

public function __construct($filepath){
$this->template = file_get_contents($filepath);
}

public function replace($var, $content){
$this->template = str_replace("#$var#", $content, $this->template);
}

public function publish(){
eval("?>{$this->template}<?");
}
}

class menu{
public $menus = array();

public function __construct($name, $url, $current){
$this->menus[$name] = $url[$current];
}
}
?>

<?php
$template = new template("layout.php");

new menu("Item 1", "layout.php", 0);
new menu("Item 2", "http://www.google.com", 1);

print_r($menus);

$template->replace("content", "This has been replaced correctly.");

$template->publish();
?>


layout.php


<html>
<head>
<title>
Title
</title>
</head>

<body>
<div>
#menu
</div>

<div>
#content#
</div>
</body>
</html>


Basically, I want to be able to use "new menu("Item 1", "layout.php", 0);" to add menu items to #menu#.

I thought it might be best to use some sort of array, but to be honest, I've no idea what would be best.

aedrin
11-14-2007, 09:07 PM
You have to seperate the different parts.

Your template needs to first collect all components, then assemble them in a predetermined way.

In your case.



template = new template
menuitem1 = new menuitem
menuitem2 = new menuitem
menuitem3 = new menuitem

template -> add menuitem1
template -> add menuitem2
template -> add menuitem3


As you guessed, arrays are the correct way.



class Template {
private $menu = array();
public function addMenuItem($item) {
$this->menu[] = $item;
}
}


Then once you ask the template class for the output, build a string with the menu code, do a search and replace for #menu# on the output. This means that you have to work with the output, so you'd have to use ob_get_contents() (http://php.net/ob_get_contents) to gather this. Then do your search and replace. Once you're done with that send the output as you would normally (using echo).

EDIT: The reason I suggested ob_get_contents() instead of just loading the template with file_get_contents, is that if you first include the template, and then use ob_get_contents(), you can still have PHP in your template. Which leaves you more options.

Jacka
11-14-2007, 09:20 PM
EDIT: Nevermind, I have worked out how to do the 2nd one.


$this->menus[$name] = array("url" => $url, "current" => $current);

Is this a sensible way to store them in the array?

It's output as
Array
(
[Item 1] => Array
(
[url] => layout.php
[current] => 0
)

[Item 2] => Array
(
[url] => http://www.google.com
[current] => 1
)

)


Or would it be better to get
Array
(
[0] => Array
(
[name] => Item 1
[url] => layout.php
[current] => 0
)

[1] => Array
(
[url] => Item 2
[url] => http://www.google.com
[current] => 1
)

)
If the 2nd is better, how would I achieve this?

Jacka
11-14-2007, 09:36 PM
EDIT: I needed to refer to $menus as $this->menus;


public function finishmenus(){
ob_start();
echo " <ul>\n";
foreach($menus as $menu){
echo " <li>\n";
echo " <a href=\"{$menu[url]}\"";
if($menu[current]){
echo " class=\"current\"";
}
echo ">\n";
echo " {$menu[name]}\n";
echo " </a>\n";
echo " </li>\n";
}
$buffer = ob_get_contents();
ob_end_clean();
$this->replace(menu, $buffer);
}

With this, I'm getting the error

Invalid argument supplied for foreach() in file on line 26

Do I have to do something fancy in the foreach?

aedrin
11-14-2007, 09:54 PM
EDIT: Nevermind, I have worked out how to do the 2nd one.

Correct, unless you need to do lookup by names, you want to store them in a plain array with a numeric index.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum