Page 1 of 2
Content management and Custom [tags]
Posted: Wed Jan 21, 2004 10:42 am
by Gen-ik
There are many ways to create a PHP based content management systems, but the best of them keep all PHP and HTML seperate so that the HTML/site layout/etc can be easily changed by non-PHPers.
One method is to include custom tags in the HTML which PHP can use to replace with various dynamic information.. and I was wondering what your thoughts on this are. Is it good? Is it bad? Is there any easier way?
As a small example this might be used in HTML to insert news items from a database...
Code: Select all
<div>
їnews]
#title
<br />
#blurb
#split="<hr />"
ї/news]
</div>
I am experimenting with a few different ideas and would be interested to hear how you tackle this problem (if it is a problem) with your own systems.
As far as I know phpBB uses a similar [tag] method.
Posted: Wed Jan 21, 2004 11:11 am
by jaxn
There is no reason to recreate the wheel. You can use one of the many high quality templating systems for PHP (including smarty, and PEAR::HTML_Template_Sigma, etc).
You are right in that these are how the templating engines typically work, but they have spent a lot of time making sure their packages are extendable enough and fast enough for use.
-Jackson
Posted: Wed Jan 21, 2004 12:59 pm
by Gen-ik
jaxn wrote:There is no reason to recreate the wheel. You can use one of the many high quality templating systems for PHP (including smarty, and PEAR::HTML_Template_Sigma, etc).
I'm not trying to "recreate the wheel" (this phrase seems very popular here at the moment). I'm building a CMS that is designed as the back-end for on-line audio stores with the main areas of design based around 12" Vinyl and CD purchases plus the on-line purchases and downloading of MP3 files. It's designed to be used by Record Labels who want to move their products into an on-line enviroment and also step into the new age of 'legal' MP3 e-commerce. A good example of this can be found at
http://www.bleep.com which is the on-line MP3 store run by Warp Records (UK).
I'm not creating 'another' Forum CMS or 'another' Blog CMS... but if I was then I would still create it myself rather than using any third-party solutions.
jaxn wrote:You are right in that these are how the templating engines typically work, but they have spent a lot of time making sure their packages are extendable enough and fast enough for use.
I'm not a Newbie to PHP or website development so I know how much time and work is involved in creating a CMS which is easy for the user to edit and customise. I am simply asking people how they go about keeping their PHP and HTML seperate. I know how to do this, I am just interested in finding out how others do it and if there are any up-sides or down-sides to the various methods used.
Thanks for your reply though.
Posted: Wed Jan 21, 2004 1:44 pm
by redmonkey
I considered using templates in the last project I did, however I came to the conclusion it was just another layer of processing I could do without. I still wanted some level of end user customization so I decided to go with an HTML layout which could easily be customized using CSS.
Posted: Wed Jan 21, 2004 2:31 pm
by jaxn
When I said don't recreate the wheel I was talking about seperating HTML from PHP, not writing a CMS.
I am suggesting you use an existing templating engine (smarty, PEAR::HTML_Template_Sigma,etc). Templating engines allow you to keep the presentation layer (HTML files) seperate from the business layer (php files) which is seperate from your data layer (database).
Your CMS then implements the templating engine to replace the place holders in the template with the data from the database (and it is your PHP scripts that does it).
Here is an example of HTML_Template_Sigma (my templating engine of choice):
template.html
Code: Select all
<html>
<head>
<title>{page_title}</title>
</head>
<body>
Here is some more info about the song you requested:
<br />
title: {title}
<br />
artist: {artist}
<br />
album: {album}
<br />
price: {price}
</body>
</html>
Then you would have a php script that is something like this:
Code: Select all
<?php
////
// get information about the requested cong from the database
// ...
// assume that info about the requested song is in the $song_info array
////
// Instantiate the template engine
require_once('HTML/Template_Sigma.php');
$tpl = new HTML_Template_Sigma('/path/to/templtes/directory/');
// load the template file
$tpl->loadTemplateFile('template.html');
// set the title of the page
$tpl->setVariable('page_title',"Information about {$song_info['title']}:");
// set the title of the song
$tpl->setVariable('title',$song_info['title']);
// set the artist
$tpl->setVariable('artist',$song_info['artist']);
// set the album
$tpl->setVariable('album',$song_info['album']);
// set the price
$tpl->setVariable('price',$song_info['price']);
// parse the template
$tpl->parse();
// display the resulting HTML
$tpl->show();
// exit gracefully
exit;
?>
I hope that is helpful. Each templating engine has their own syntax and can handle things like nested tables, repeating blocks, etc.
I wasn't trying to insult your knowledge or belittle your project. You might want to see how people are trying to be helpful when they reply to you and not assume that they are working against you.
-Jackson
Posted: Wed Jan 21, 2004 3:14 pm
by Gen-ik
@ jaxn
Thanks for the help mate. I wasn't having a go at you or anything.
I'm using a similar method myself at the moment where PHP replaces 'tags' within HTML pages. The user can edit these HTML pages (or create new ones) and as long as they have the right tags where they want the information to appear then it all seems to work fine.
The user can also set-up 'blocks' of HTML to use as well, like a small section of HTML to display each product... and this block then gets used/repeated by PHP when and if it needs to be.
Thanks again.
Posted: Wed Jan 21, 2004 8:46 pm
by McGruff
Hunt around for "template engine", "smarty" and so on: there was quite a good thread somewhere on phpdn a while back. Just don't pay any attention to what I might have had to say back then.
The basic aim, as I see it, is to print a page using html templates (or pdf, xml etc) which echo out php vars. Regex & custom tags essentially does the same thing - although you can get more complex. In either case it's easy for an html designer with no knowledge of php to make layout edits by opening up separate template files.
If for some reason you can't trust everyone with design access not to try to hack the program, use placeholders & regex so that you can also strip out any scripting which you don't want to allow. If this isn't an issue, just <?php echo $var; ?> and save on the regex'ing.
Posted: Thu Jan 22, 2004 6:31 am
by malcolmboston
jaxn wrote:There is no reason to recreate the wheel. You can use one of the many high quality templating systems for PHP (including smarty, and PEAR::HTML_Template_Sigma, etc).
-Jackson
i looked at this and thought is that the way we should be thinking?
if we all just left it then where would all the creativity, so we have one definitive PHP forums (PHPBB) why bother even trying to make another one, well because its fun
personally no matter how good a script is i always try and write my own, its much more rewarding
Posted: Thu Jan 22, 2004 6:33 am
by malcolmboston
Gen-ik wrote:
I'm not creating 'another' Forum CMS or 'another' Blog CMS... but if I was then I would still create it myself rather than using any third-party solutions.
Posted: Thu Jan 22, 2004 10:44 am
by jaxn
malcolmboston wrote:
i looked at this and thought is that the way we should be thinking?
if we all just left it then where would all the creativity, so we have one definitive PHP forums (PHPBB) why bother even trying to make another one, well because its fun
It may be fun, but it doesn't really further the community. Coding for educational purposes is fun and it is nice to know how things work. By solving the same set of problems over and over again we evolve at a slower rate.
Having said that, it is better to improve upon phpBB than to recreate phpBB if your goal is to create something of value to the community. If you want to make changes that the phpBB leaders don't support than fork the code. If you want to create something drastically different then you aren't reinventing the wheel in the first place.
If everyone created their own methods for authentication then it would be much harder to integrate apps. But we need the variety of LDAP, SASL, Active Directory, htpasswd, etc. So whenever possible use one of those (or make you app support multiple protocols). If you need something that existing tools/protocols don't support, THEN you write your own.
malcolmboston wrote:
personally no matter how good a script is i always try and write my own, its much more rewarding
And that is great, but I feed my family from writing code. So for me, developing solid applications that integrate with other applications quickly and efficiently is the goal. I will never write another templating system unless my needs aren't met by existing classes/packages (in which case I will probably work to make an existing package do what I need and submit the changes).
If you don't want to use existing code then start with assembly and write a bios. Then move to C and write a kernel. Then build an OS on top of the kernel. then build a language interpreter to replace php. Oh wait, maybe you better start by writing an assembly language for a chip that you design yourself. That would be much more rewarding than writing a templating system.
I am being a little too sarcastic, but I promise it is well-intentioned.
-Jackson
Posted: Thu Jan 22, 2004 2:10 pm
by Gen-ik
@ jaxn
There are a few good reasons why it's good to use existing code (as you've pointed out) but there are also an equal number of reasons why it's good to write your own code from scratch.
For example the project I'm working on at the moment needs to be template based to allow potential clients to easily change the look and layout of the end result without any knowledge of PHP... which is the whole point of template systems.
I'm creating my own template system from scratch for a number of reasons with the main ones being (a) there won't be any un-used or un-needed code laying around like you get with third-party systems (b) it's being designed as the back-end of an on-line audio/record label store so it needs to include a lot of functions dedicated to the job (c) it's not being created for 'the community', it will be used as part of my personal website development arsenal.
Progress requires redesign. New technology and methods of working are created by people experimenting with new ideas or resigning existing technologies... regardless of if it's to 'improve' on an existing idea or not.
However I don't want to turn this topic into a big debate... in my orginal post I simply asked about the various ways of keeping HTML and PHP apart... and I would like to keep that on track.
Posted: Fri Jan 23, 2004 6:58 pm
by ilovetoast
I second your arguments for coding your own. I also second others' ideas about forking exisiting codes. For example, I love PEAR, but I use my own forked version. Too much depricated bloat in the main development line (for reasonably good reasons) for my needs. The advatange I find is that my classses work with my forked-PEAR and with vanilla PEAR. Let's me release things to the community as a whole where I feel like it, and be comfortable knowing that the code works with the standard.
Back on topic, I do something similar, but set it up like this:
PHP --> XML --> HTML, WML/WAP, Proprietary App, etc.
Makes a little more work, but you might find it useful. Particularly if your backend needs to feed to multiple clients on assorted protocols. I have PHP write to XML templates and then move forward with those in whatever direction I need to go.
peace
burnt toast smells
Posted: Sat Jan 24, 2004 9:53 am
by timvw
Although i don't like his arrogant style, there are some interesting things to read at tonymarston.net on setting up a webdevelopment framework in his php section.
But it all comes down on your needs and what you're trying to do. Imho is the wrapping of data into xml to pass it from the model to the view usually just another source of overhead.
Posted: Sat Jan 24, 2004 11:37 am
by Gen-ik
timvw wrote:Imho is the wrapping of data into xml to pass it from the model to the view usually just another source of overhead.
This is true. The only reason I have ever needed to use XML is to create a 'news-stream' for a site I worked on once... and that's it!
I think there are a lot of people using XML at the moment simply because it can be used... the same way that some people cover their sites in Flash content (menus etc) when HTML would do the job just as well and would, in most cases, be quicker and easier to change.
Programming has it's "fads" just like fashion does (remember those t-shirts that changed colour when they got warm)... and people do tend to jump on the "fad" band-wagon wether it's code or clothes.
But that's another topic altogether

Posted: Sat Jan 24, 2004 11:56 am
by ilovetoast
If all it's ever going to do is be spit out as HTML then XML is overkill as you said. My thought just was that since you mentioned this was a back-end there might either already now be (or potentially in the future) some alternate presentation mode.
I go XML at times when data can be accessed a variety of ways, which are either in use or will be in the near future. In those cases, web visitors see it as HTML, wireless visitors via WML, and also any stand alone app that can use the XML will be able to display/process it as well. XML makes it easy to go in any of those directions from one source file.
That said, if all you're shooting for in the final analysis is HTML or even WML+HTML then I wouldn't even bother with XML as its just a waste of effort, IMHO.
peace
grape jelly on toast
edit: my grammar is good.