Separating program and artwork in PHP - easier maintainance, and better for the user
I can code reasonably well. But my graphic design sucks. And both skills are needed for even a simple web data entry form, or a small application.
Using the PHP "bull at a gate" approach, both business logic (the code) and look and feel (the graphic art stuff) can be written into a single file, but it rapidly gets had to manage both elements together, and some sort of separation into two files - which can be maintained by different people, on a different maintainance cycle, makes sense.
During last week's
PHP course, I split a really simple example across two files to show how the concept of spliiting in this way starts in practise.
Firstly, a "template file" is written - that's the HTML, but with a unique marker which says "this is where the result of running the code goes". I've chosen to use the string
%result% as this marker - it's easily recognised, unlikely to ever really be needed in the template, and can start to form a pattern when I have a whole array of markers to be replaced. Here's part of the template showing it:
<h1>Multiplication tables</h1>
%result%
<hr />
Second, in the business logic - which is a pure PHP file without any HTML code at all, I do NOT use any
print or
echo calls, but rather produce variable(s) containing the programatically changing element of the page. In my example, I've used the very unimaginative name
$string for this variable. Then at the end of the code, I read in the template, substitute the %result% string for the contents of $string, and output the result. Here's the extra code for that:
$stuff = file_get_contents("t2.xxx");
$stuff = preg_replace("/%result%/",$string,$stuff);
print ($stuff);
This principle can be extended to include multiple fill in fields using an array and a loop of
preg_replacees. And extended further to separate out a function to do the actual template completion (common code shared between lots of applications). And extended still further to a mulipage, session based application which shares the same template between each of its pages ... giving an application that's easy to maintain, and friendly and familiar to use. Truly, this example is the start of something good - and you should start as you mean to carry on.
Source of the template file
[here]
Source of the business logic
[here]
And, yes, I'm a programmer. The code is reasonable. Look and feel naff!
These ideas are introduced on our
Learning to program in PHP course, and the alternative
PHP Programming course for delegates who have prior programming experience. They are further developed on our
PHP Techniques Workshop which shows interemediate PHP delegates how to make the very best of the language and system.
(written 2011-12-05, updated 2011-12-06)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H302 - PHP - MVC, 4 layer model and templating [1634] Kiss and Book - (2008-05-07)
[1716] Larger applications in PHP - (2008-07-22)
[1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
[2174] Application design in PHP - multiple step processes - (2009-05-11)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
[3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
[3956] Zend / layout of MVC and other files in an example application (PHP) - (2012-12-16)
[4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
[4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
[4314] PHP training - refreshed modern course, backed up by years of practical experience - (2014-11-16)
H115 - Designing PHP-Based Solutions: Best Practice [123] Short underground journeys and a PHP book - (2004-11-19)
[237] Crossfertilisation, PHP to Python - (2005-03-06)
[261] Putting a form online - (2005-03-29)
[340] Code and code maintainance efficiency - (2005-06-08)
[394] A year on - should we offer certified PHP courses - (2005-07-28)
[426] Robust checking of data entered by users - (2005-08-27)
[563] Merging pictures using PHP and GD - (2006-01-13)
[572] Giving the researcher power over database analysis - (2006-01-22)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[945] Code quality counts - (2006-11-26)
[1047] Maintainable code - some positive advice - (2007-01-21)
[1052] Learning to write secure, maintainable PHP - (2007-01-25)
[1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[1182] Painting a masterpiece in PHP - (2007-05-10)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1323] Easy handling of errors in PHP - (2007-08-27)
[1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1482] A story about benchmarking PHP - (2007-12-23)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1490] Software to record day to day events and keep an action list - (2007-12-31)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1623] PHP Techniques - a workshop - (2008-04-26)
[1694] Defensive coding techniques in PHP? - (2008-07-02)
[1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
[2430] Not just a PHP program - a good web application - (2009-09-29)
[2679] How to build a test harness into your PHP - (2010-03-16)
[3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
[3820] PHP sessions - a best practice teaching example - (2012-07-27)
[3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
[4069] Even early on, separate out your program from your HTML! - (2013-04-25)
[4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
Some other Articles
Some different pictures from MelkshamWhat order are operations performed in, in a Perl expression?I loves Melksham Easy session example in PHP - keeping each customers data apartSeparating program and artwork in PHP - easier maintainance, and better for the userSanta Train - another successful trip - 4th December 2011Letting the foster parents know ... too little or too much?UK Mapping Data - and more to come - under government Open Data measuresMelksham, Luxury Hotel Rooms, Christmas.Learning to program in PHP - Regular Expression and Associative Array examples