Jump to content
xisto Community
Sign in to follow this  
matak

Php Template Class I Started and a good way to see how to fail template class

Recommended Posts

Althought it works, and maybe some users can even use this class for some simpler websites, this is not a good way to create class. I cant really explain why, and i'm publishing it so maybe people with more experience in template design can help me figure out how to create PHP templates. Looking forward for your answers.

some of the variables and directories are in croatian, but i don't think that matters when you look at with programmers eyes.

First my index.php file looks like this...

<?php 	include ("includes/showSiteClass.php"); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://forums.xisto.com/no_longer_exists/ xmlns="http://forums.xisto.com/no_longer_exists/; xml:lang="en" lang="en"><head><title>jednostavno.com - Jednostavno o Web dizajnu i programiranju - <?php $ypSiteModules->showTitle(); ?></title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><link rel="stylesheet" href="css.css"><style type="text/css"></style></head><body><div id="gore"><?php $ypSiteModules->showMainLinks($_GET); ?></div><div id="lijevo"><?php $ypSiteModules->showSubLinks($_GET); ?><div id="desno"><?php $ypSiteModules->showContent(); ?></div></body></html>

showSiteClass.php is this

<?php error_reporting(E_ALL); ?><?php	class c1ShowContent {				private $variable = array (				 "index"	=> array ("PHP Open source sustav" => "opensource"),									"HTML" => array ("Pisanje HTML koda [HTML 0001]" => "pisanjehtmla"),									"PHP"	=> array ("Instalacija Xampp [PHP 0001]" => "instalacijaxampp"	)									);			public function showMainLinks($fun) {			echo "<div class=\"mainLinkovi\"><ul>";			foreach ($this->variable as $key => $val) {				echo "<li><a href=\"?main=".$key."\">$key</a></li>";			}			echo "</ul></div>";			$mainKeys = array_keys ($this->variable);								if (empty($_GET['main'])) {				$this->link = $this->variable["index"];				$this->main = $this->variable = "index";			}			elseif (in_array($_GET['main'], $mainKeys)) {				$this->link = $this->variable[$_GET['main']];				$this->main = $_GET['main'];			}			else {				$this->link = $this->variable["index"];				$this->main = $this->variable = "index";			}				}				public function showSubLinks($fun){			echo "<div class=\"subLinkovi\"><p>Sadraj - $this->main</p>";			echo "<ul>";			foreach ($this->link as $key => $val) {				echo "<li><a href=\"?main={$this->main}&sub=".$key."\">$key</a></li>";			}			echo "</ul></div>";			$subKeys = array_keys ($this->link);			$this->subKeys  =$subKeys;						if (empty($_GET['sub'])){			$this->content = $this->main;			}			elseif (in_array($_GET['sub'], $subKeys)) {			$this->content = $this->link[$_GET['sub']];			}			else {			$this->content = $this->main;			}		}					public function showContent(){			include "stranice/$this->main/$this->content.html";						$filename = "stranice/$this->main/$this->content.html";				if (file_exists($filename)) {					echo "<p class=\"ftime\">Dodano: " . date ("d F Y H:i:s.", filemtime($filename))."</p>";					}		}				public function showTitle() {					$mainKeys = array_keys ($this->variable);												if (empty($_GET['main'])) {			$this->title = "index";			}			elseif (!empty($_GET['main']) && empty($_GET['sub'])) {			if (in_array($_GET['main'], $mainKeys)){			$this->title = $_GET['main'];			}			else {			$this->title = "index";			}			}			elseif (!empty($_GET['sub'])) {			$this->link = $this->variable[$_GET['main']];			$subKeys = array_keys($this->link);			if (in_array($_GET['sub'], $subKeys)){			$this->title = $_GET['sub'];			}			else {			$this->title = "index";			}			}				echo $this->title;		}			}$ypSiteModules = new c1ShowContent;?>

directory sturcture on root is

-includes
--showSiteClass.php

-stranice
--index
---index.html
---opensource.html

--HTML
---HTML.html
---pisanjehtmla.html

--PHP
---PHP.html
---instalacijaxampp.html

index.php

Problem i found on this class, that it's not dynamic enough. It's fast, and i think it's secure enough, but i got some problems when i wanted to include some pages EG. contact.php, that need another included file eg mailto.php, and can't figure out a way to solve that problem, except by including it in index.php file at start, but than it would be availiable to any file i have, and that's not an option.

I tried to find many good tutorials on writing PHP template system, and am trying to avoid smarty, beacouse i preffer to use my own templating system until i become more familliar with the way it works.

If you had experience in building such a system, please help.

Main problem i think is, array that sets pages, beacouse it's really crude for now, and i need to make it more justifiable (dunno the proper word).

Thanks for any help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.