I have a web application developed using PHP. I want my users to select themes for their pages throughout the application.

  • Where should I start before using PHP themes?

What should I know about Themes in a PHP application?

Edit:

My question about themes is only about changing the color of the layout, not the images.

Suppose My ADMIN user will have white and Orange, but my staff user will have white and green...

How it can be done in PHP with CodeIgniter?

Accepted Answer

There are lots of directions you can go with this.

1) "CSS ZEN"

This is where the markup remains unchanged, but you totally change the design just by using CSS and images. Demonstrated very well on http://www.csszengarden.com/

2) MVC Stylee

This is where you create a model that represents the page data and then pass it to a view, which contains some inline echo statements. The idea is that you could send the same model to a totally different view so it could look entirely different, HTML and all. Cake PHP is a good start for this: http://cakephp.org/

Example:

<div class="content">
    <? echo $Page->Content ?>
</div>

3) Micro-Markup

With this method, you add your own "special tags" to an HTML page. You then read in your plain HTML page and replace the special tags with the information you want to display. This is good if you want your templates to be recognisable to HTML guys that don't know PHP and might break the PHP code in the MVC app.

Example:

<div class="content">
    <#Content#>
</div>

Out of all of these, MVC is a very structured way of achieving what you want - however, I listed the other options as they cater for specific scenarios that might be relevant to you.

I have implemented the concept in all three of these, in situations that were appropriate for each.

Regarding The Edit In The Question

I imagine you'll have "something" that represents your user - so it is as easy as:

(In the event of just wanting to override a few settings...)

<link href="style.css" type="text/css" rel="stylesheet">
<?php if ($User->Type === USER_ADMIN) {  ?>
<link href="admin.css" type="text/css" rel="stylesheet">
<?php } ?>

You can adjust this example in the following ways:

  • Use a switch statement if there will be many user types
  • If the replacement is total, rather than just a few overrides, you may want to completely swap the stylesheet.
Written by Sohnee
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki