How to get started with PHP themes?

I have a web application developed using PHP. I want my users to choose themes for their pages in the app.

  • Where to start before using PHP themes?

What do I know about topics in a PHP application?

Edit:

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

Suppose my ADMIN user has white and orange, but my user has white and green ...

How can this be done in PHP with CodeIgniter ?

+4
source share
6 answers

There are many destinations you can go with.

1) "CSS ZEN"

Here the markup remains unchanged, but you are completely redesigning just using CSS and images. Demonstrated very well at http://www.csszengarden.com/

2) MVC style

Here you create a model that represents page data and then passes it to a view containing some built-in echo instructions. The idea is that you can send the same model to a completely different view so that it looks completely different: HTML and that’s it. Cake PHP is a good start to this: http://cakephp.org/

Example:

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

3) Micro-marking

Using this method, you add your own “special tags” to the HTML page. Then you read on your simple HTML page and replace the special tags with the information you want to display. This is good if you want your templates to be recognizable for HTML guys who don't know PHP and can break PHP code in an MVC application.

Example:

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

Out of all of this, MVC is a very structured way to achieve what you want - however, I have listed other options as they are for specific scenarios that may be relevant to you.

I implemented the concept in all three of them, in situations that were suitable for everyone.

Regarding editing in question

I assume you will have “something” representing your user - this is easy:

(In case you just need to undo 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 } ?> 

This example can be configured as follows:

  • Use a switch statement if there are many types of users
  • If the replacement is complete, and not just a few overrides, you can completely change the stylesheet.
+4
source

Typically, you created template files containing HTML and CSS and built PHP generated values ​​at run time.

The best approach to this is to keep the theme in a separate directory containing no code, just template variables like {mainmenu} , {backbutton} , {content} ... you get my drift. They are then populated with your PHP script, possibly using a template engine such as Smarty (No need to reinvent the wheel here).

There is also an approach related to PHP markup directly in the template file, for example echo $xyz; , while this is a perfectly good practice, which I often use, I recommend using the template engine using PHP markup in the code, if you want a solid, future template system, because:

  • Firstly, there are fewer features that a designer may break when working with HTML.

  • Secondly, the presence of PHP markup in the code is the temptation to program the PHP logic inside the template (loops, conditions) instead of properly preparing them in the PHP code at the point where the template variables are created. This is terrible for serving and continuing to use your templates, because you need to replicate this PHP soup again in every new template. In the end, you want to have a template engine so that others can create new perspectives on your product without knowing how to program it.

  • Third, when using a template-based approach, you have the option to add caching where necessary without additional work . Unable to use script. Yes, in a web application you won’t be able to cache a lot, but with a lot of data there will be times when this will help the user.

  • Fourth and least important, it makes your template less easy to export to other applications and imports templates from other applications.

The Zen Zen approach mentioned by Sohnee is great for websites, but would be too limited for a web application that uses complex input elements, JS menus, etc. Too often that these elements need to be changed and customized in the markup.

+3
source

If you look at my CodeIgniter template library , it briefly explains how you can customize themes and layouts (equivalent to title and footnote).

If you set up a global code, for example, Hook or MY_Controller , then you can dynamically customize your theme based on the user's login, type of user, etc.

For instance:

 if($user->role == 'admin') { $this->template->set_theme('admin_skin'); } else { $this->template->set_theme($user->theme); } 

This is just a VERY basic example of what you can use this template library for.

+1
source

CMS Solutions

Magento and Wordpress pack all theme-related files into their own separate directories. They contain server code, stylesheets, images, and javaScript for the theme. The user actually selects the directory that will be used, which affects how the page is laid out and designed.

Simpler approach

An easier way to get started is to accept that actual content, for example. The HTML page will remain the same, but it allows the user to choose from various CSS stylesheets.

When choosing a stylesheet, the system can use JavaScript to dynamically load it so that the user can view the appearance that they choose.

0
source

If you have really good semantic HTML, changing CSS files is enough. If the design changes are really heavy. Then it would be advisable to provide PHP templates that are created using some modules: variables that contain a certain HTML structure, such as navigation or sidebar, etc.

0
source

For those you do not need PHP. Just define your themes in CSS (the best way is one file for each theme) and use a simple JavaScript selection, as on this site: http://www.fotokluburan.cz/switchcss.js .

0
source

Source: https://habr.com/ru/post/1299934/


All Articles