Theming a site

Separating logic and presentation

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 3999 Views - Last Post: 04 October 2009 - 01:51 PM

#1 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Theming a site

Posted 27 September 2009 - 06:31 AM

What I am trying to do is create an effective way of removing most if not all logic from the presentation part of my code.

I am trying to create a CMS - a relatively simple one, but one that meets all of my requirements. I know that there are plenty of worthy candidates out there, as well as theme engines and PHP frameworks - but I would really like to do this without them in order to try and better myself and generally take on the challenge and see what I can come up with. A lot of you may not see the point in this but it's something I want to do.

I have my basic functions sorted, I have basic login functionality and am coming onto presenting a main page onto the site. I want all of the pages where possible to be dynamic e.g. stored in the database hopefully allowing almost if not everything to be customised by changing what is stored in the database.

I am looking for suggestions on how you would achieve allowing everything to be dynamic and separating the themed HTML & CSS from the actual PHP logic code. Like most CMS's I want it to be themed allowing users to change the theme from the front end providing a compatible theme has been created and placed in the correct place.

I don't really need the code just some idea's, like I said I actually want to code this myself or I would just head on over to a framework and use their functionality

My most successful idea so far was a rather un-elegant method of having loads of HTML helper functions which are defined in the theme.php file for rendering content, but I haven't allowed for that much yet and it's already bordering unmanageable.

Any help would be appreciated
Cheers
Danny

Is This A Good Question/Topic? 0
  • +

Replies To: Theming a site

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 27 September 2009 - 06:52 AM

I think you should take a look at the MVC (Model-View-Controller)-pattern which would really, in my opinion, come in handy for this kind of an idea:
http://www.tonymarst...controller.html
http://www.phpro.org...roller-MVC.html, Google "PHP MVC" and you'll find a lot of nice stuff! :)

To make everything dynamic would mean to store all (X)HTML in a database, is that really what you want? :)

This post has been edited by Wimpy: 27 September 2009 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 27 September 2009 - 07:37 AM

Kind of. I still want the theme and any XHTML related to how the main aspects of the site looks to be in physical files in a theme directory of the site. But as far as content goes I want it all in the database, this will in my opinion make it easier to search later.

Really although I said I wanted all of the pages in the database, I want all of the content in the database, meaning really XHTML should be simple stuff and could even possibly be replace using simple BBCode tags. Similar to articles in many content management systems

If we take an example theme: (how it looks is not important it was a random selection) http://www.freecsste...review/republic

The top blue bit would probably allow for logo, site name, tag line etc so it would need to be dynamic to a degree - if not fully??

Navigation will also need to be dynamic. The trend is to use <ul> tags for but it still needs to be dynamic. Probably a table for navigation entries?? There are also commonly different CSS classes and styles applied to the tags, I would rather not force users to use specific class names as this make porting or creating themes much harder than it really needs to be.

We can clearly see that the content is split up into "posts" i want to support that format, but not require it for potential articles/pages that it is not suited for.

Sidebar, I want the search but obviously not all themes have that layout so it needs to be customisable through theme including potential CSS classes etc. I also would like to be able to disable it through a site setting. Sidebars also tend to use the unordered list tag again and some also have more advanced navigation which I want to support, but not require which shouldn't be that difficult.

Footer is often just some text so it will probably be addressed by a simple setting. I am thinking that themes that require a copyright notice should be able to set this in the theme file so it will be displayed without any way of modifying this through the administration panel - only by modifying the theme's code.

I know that seems complex but it will probably be the most complex aspect of the site and in my opinion it's better for it to be complex and allow expansion that it to be the main cause of restriction which will inevitably mean a lot of reworking should I want to expand it later.

Cheers,
Danny

This post has been edited by danny_kay1710: 27 September 2009 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 27 September 2009 - 08:15 AM

I think you are doing this too big, start small and let it grow big! :) Create a small CMS, with little but great functionality and make everything as abstract as possible so that it is easy to build upon. When you have a solid ground to stand on; expand and evolve! :) I don't know what else to say, but if you try to make this very big project right away you will most likely have to redo it all at some point! :)
Was This Post Helpful? 0
  • +
  • -

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 27 September 2009 - 01:36 PM

That is what I am trying to do, but my thinking at this point is that if I get the theming aspect of the site wrong it will lead to me having more work to do as it grows.

I want a solid, simple and expandable framework and that is what I am struggling to come up with so far.

I already have a simple implementation of what I want but the theming engine is basically non-existent and it also has the drawback of being procedural and I would prefer it to be OO. In my opinion procedural programming doesn't suit a project like this, and is in less maintainable.

What would you scale down? Or more appropriately what would you focus on?
Was This Post Helpful? 0
  • +
  • -

#6 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 27 September 2009 - 02:10 PM

I would start to focus on getting a single-point-entry to work e.g. all request goes to one specific file that routes the request and validates the user performing the request. So far it shouldn't matter how you store the information or where as long as you make it easy for yourself to change it in the future.

My second priority would be user "authing" (authentication and authorization), who should be able to access what and to what extent?

After that I would think about ways to store my presentational content and client-side behavior (html, css, javascript, etc). As of now I have it multiple ways, some pages on my sites are not dynamic at all while some of them are completely dynamic and others are a mix of both. This is a part of the system that you should let the client coder decide instead of having it determined by your CMS. The CMS on the other hand should have some particular interfaces or abstract classes that must be implemented in some way to make the client coders way of doing things fit the CMS.

Coming this far and having a solid foundation I would build other functionality into the CMS, and this could be anything, like a structured way of having a multilingual site or something!

This is just my way of thinking and I guess that there are a billion people out there thinking otherwise... :)

This post has been edited by Wimpy: 27 September 2009 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 28 September 2009 - 12:18 AM

OK, I think I am ahead of you.

All of my PHP websites, procedural or OOP use a single index.php as their point of entry and I tend to use mod_rewrite to make the URL's friendlier. I already have this done. generally something along the lines of index.php?page=contact_me

I also already have authorisation sorted - a relatively simple guest, member and admin user model.

As far as presentational content anything regarding extra PHP code will need to be file based but standard static content I wanted to be able to be in the database there is no harm in allowing for both. No PHP code in my opinion should ever be stored and retrieved from a database if it can be helped.

I quickly ported an old procedural site to my new domain just as something whilst I am working on the full conversion to OOP: www.danielbkay.com

When I get a base for the OOP version it will replace the one currently live and as features are programmed I will bolt them on. The live site features include theming (through non elegant & restrctive theme engine), dynamic pages, dynamic navigation, user authentication just all without a proper admin backend meaning its a database edit job at the moment)

The main reason why I have posted this is because it is something I face nearly every time I come to develop a site, as of yet I have not come up with a way to theme a site that I am completely happy with - maybe it's me being picky, or always overlooking the same thing.

Cheers
Danny

This post has been edited by danny_kay1710: 28 September 2009 - 12:20 AM

Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 28 September 2009 - 05:08 AM

ok, I have to ask, since I'm not completely following you when it comes to exactly what it is you want to be able to theme? Only colors and images (CSS (mostly))? Or even the structural layout (HTML)? And you want this to be done without letting the users fiddle with the code itself, am I wrong?

This post has been edited by Wimpy: 28 September 2009 - 05:08 AM

Was This Post Helpful? 0
  • +
  • -

#9 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 28 September 2009 - 05:42 AM

OK, sorry If I'm not being very clear.

Have you used PHP Fusion or other similar CMS. I believe Joomla behaves the same way. You simply download the theme which normally contains the CSS and accompanying images and a theme file which normally determines the structural layout with places where content will be added.

I want to achieve similar, if possible. And yes, I would like the themes to be separate from the main CMS code but that doesn't mean they won't have to manually edit code to make the theme. That make sense? There will not be a way to modify a theme from the site (but you will be able to change to a completely different installed theme)

Essentially I would be able to drop a new theme into a themes folder and then login and change it to that theme

Hope that's cleared things up.
Danny
Was This Post Helpful? 0
  • +
  • -

#10 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 28 September 2009 - 06:02 AM

I intend to try out Joomla and Typo 3 some day but no I haven't actually, but I get your idea now! :) And I think you are quite clear, just me not being a native english speaking SOB so I might get lost on the way. :)

You would have to add some lines of PHP to your themes like <?php echo $this->MainContent; ?> or something, your idea would, in my opinion, fit quite well with a MVC pattern, did you check up on that? :)
Was This Post Helpful? 1
  • +
  • -

#11 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 28 September 2009 - 06:32 AM

I did and strangely I found my coding model to fall relatively close to the model it was describing, despite having never looked into it.

Well that's kind of what I have opted for so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?php echo $this->getTitle(); ?></title>
<meta name="keywords" content="<?php echo $this->getMetaKeywords(); ?>" />
<meta name="description" content="<?php echo $this->getMetaDescription(); ?>" />
<link href="<?php echo $this->getThemeCSSPath(); ?>" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="bg1">
    <div id="header">
        <h1><?php echo $this->getHeader(); ?></h1>
        <h2><?php echo $this->getTagLine(); ?></h2>
    </div>
    <!-- end #header -->
</div>
<!-- end #bg1 -->
<div id="bg2">
    <div id="header2">
        <div id="menu">
            <?php echo $this->getNavigation(); ?>
        </div>
        <!-- end #menu -->
        <div id="search">
            <?php echo $this->getSearch(); ?>
        </div>
        <!-- end #search -->
    </div>
    <!-- end #header2 -->
</div>
<!-- end #bg2 -->
<div id="bg3">
    <div id="bg4">
        <div id="bg5">
            <div id="page">
                <div id="content">
                    <?php echo $this->getContent(); ?>
                </div>
                <!-- end #content -->
                <div id="sidebar">
                    <ul>
                        <?php echo $this->getSidebar(); ?>
                    </ul>
                </div>
                <!-- end #sidebar -->
                <div style="clear: both; height: 40px;"></div>
            </div>
            <!-- end #page -->
        </div>
    </div>
</div>
<!-- end #bg3 -->
<div id="footer">
    <?php echo $this->getFooter(); ?>
</div>
<!-- end #footer -->
</body>
</html>


What I have is a themeEngine.php file which is a theme handler class and at the moment on load it includes using a require_once the corresponding theme.php for the theme that it is set to use. The included theme.php contains 3 function formatPost(), noticeText() and drawTheme()

drawTheme is the above function, formatPost allows for a formatted post/blog style entry like most themes tend to offer and noticeText is what will be used for validation and other errors and will be placed above the content within the content section.

I should probably change the theme.php to a new class that extends or inherits themeEngine but for now I am sticking with what works!!

Thanks for all your help, I think I may finally have something workable. It still has it's limitations but I don't think any solution would have none at all.

It seems to have fallen in quite nicely with my other classes. I also think this has given me an idea for how to implement a basic plugin-like system which will allow extra sections to be added easier.

Cheers
Danny
Was This Post Helpful? 0
  • +
  • -

#12 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 28 September 2009 - 06:39 AM

No problem, happy to help! :)

---

I see you use a lot of functions as getters, have you considered using the magic methods __get() and __set() to shorten up the syntax a little? :)
Was This Post Helpful? 0
  • +
  • -

#13 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 28 September 2009 - 07:56 AM

View PostWimpy, on 28 Sep, 2009 - 12:39 PM, said:

No problem, happy to help! :)

---

I see you use a lot of functions as getters, have you considered using the magic methods __get() and __set() to shorten up the syntax a little? :)



I will be using a mixture of them and what I am using now because certain functions including the content and sidebar will probably do more then just retrieve a value in the end and so echo <?php $this->getContent(); ?> will probably change to <?php $this->getContent(); ?> and getContent() will be responsible for echo'ing the appropriate content out.

Cheers
Danny
Was This Post Helpful? 0
  • +
  • -

#14 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Theming a site

Posted 28 September 2009 - 08:09 AM

Then you're almost doing it like me (I think), this is my typical class-properties setup:
class User
{
	protected $userName;
	
	public function __construct($userName)
	{
		$this->UserName = $userName;
	}
	
	public function __get($key)
	{
		switch($key)
		{
			case "UserName":
				return $this->getUserName();
			default:
				throw InvalidArgumentException("Property ( $key ) does not exist.");
		}
	}
	
	public function __set($key, $value)
	{
		switch($key)
		{
			case "UserName":
				$this->setUserName($value);
				break;
			default:
				throw InvalidArgumentException("Property ( $key ) does not exist.");
		}
	}
	
	public function getUserName() { return $this->userName; }
	public function setUserName($value)
	{
		// Code for validating value comes here...
		
		$this->userName = $value;
	}
}

The benefits, I think, is that you get the possibility to validate the data in the setter as well as the shorthand syntax to retrieve and set the values from both client and internal code. :)

This post has been edited by Wimpy: 28 September 2009 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

#15 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Theming a site

Posted 28 September 2009 - 03:07 PM

It's not quite what I was thinking of, but it is a good idea - one I might adopt depending on how things work out.

Site is relatively simple in structure and will hopefully allow for easy add-ons for expandability.

Currently 6 classes, an entry point and a theme (which consists of a theme.php plus CSS & images).

Main 3 classes are:
site class - used to encapsulate all other objects, the glue of the website if you will
database class - connects to database as the object is created, closes connection as it is destroyed. Provides query functionality.
validator class- used for validation, provides a way of adding custom error's for general 'friendly' error feedback to user

The other three all get passed a reference of the site class in it's constructor:
pageEngine class - a page class is the default controller. Basically when nothing is selected in the GET vars of the URL to change controller (e.g. to news or blog) then this class handles it. So index.php?c=page&value=default would give the same output as index.php?value=default simply because when C was not specified it would default to this controller. Specifying blog as the 'c' variable would ineligibly pass it on to a blogEngine or similar (should make it easier for mod_rewrite and allowing ordinary pages to not appear to be in a subfolder.)
themeEngine class - a class to allow for theming. Holds content in variables until the drawTheme() function is called when it will render the page with the content in appropriate place
userEngine class - a login class, providing all the functionality required to authenticate users. Has simply 3 levels Guests, Members and Admins so far.

If there was interest I could upload it, but I would need to tidy it up - it's incomplete, not fully commented and just generally needs some tidying up.

Danny

This post has been edited by danny_kay1710: 28 September 2009 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3