How to update your website using a simple content management system
In and out
One of the problems with adding information via a CMS is how exactly do you add
it? Do you just allow people to enter text, or do you make provisions for
headlines and so on?
Some systems allow you to simply add a headline, and then body text. If you want a subheading, you have to add an extra element, then add another element of body text, and so on, which can be fiddly for a novice.
But then, a block of plain text is awkward too. And what if the original is already formatted, for example, in Microsoft Word?
You could allow users to save pages as HTML from Word, and allow a simple way to upload them.
But what if they have used odd fonts? You could end up with some parts of the site looking truly horrible.
So, the solution I’ve adopted – and remember it might not be suitable for everyone – is to simply allow text to be cut and pasted, and then to use codes to detect when line breaks or headings should appear.
You could, of course, try to detect line breaks with a script, but you then run the risk of them not being consistently used in the original document.
While this is a little more than a straight cut and paste, it’s not too much work.
What I’ve ended up with is something quite simple, where I can enter suitably formatted text into a database field, directly or via a web form.
It’s fairly simple then to change a document, skimming through it and adding the necessary codes.
You could even do a search and replace in Word for the end of paragraph symbol, for instance. But how do you get it onto the web page?
Selecting it from the database is pretty straightforward. Then you need to turn those codes back into something that’s usable for the browser.
To do that, I have a small PHP library included in the pages that need to display database content.
There’s a formatPage function that takes two parameters: the first is the string retrieved from the database, and the second says where on the page it’s going – whether it’s for the right-hand panel, or the main body – so that the correct CSS style names can be used.
In a typical Dreamweaver-style query-based page, all you have to do is say
something like:
echo formatPage($row_Content[‘pageText’],’body’) ;
Later in this article, under 'The formatPage function', you will see the code for the formatting function.
As you can see, it’s a simple use of Perl-compatible regular expressions, and you could do the same in your own favourite scripting language.
It’s a touch simplistic too; for example, it doesn’t allow for punctuation in headings, but for many sites something such as this could be a quick solution to making it easy to keep pages up to date.
THE FORMATPAGE FUNCTION ’ . $pageInfo . ‘< /p
>’ ;
function formatPage( $pageInfo, $where ) {
// format the text with appropriate CSS and HTML tags
// we use @@ to indicate end of para
// links are formatted with WWW:site title,URL:www.whatever
// and headings with %%heading%%
// code is added by using *+ to begin a block
// and *- to end it, with ** as a
if ( $where == ‘panel’ ) {
$para = ‘rhText’ ;
$link = ‘rhLink’ ;
$head = ‘’ ;
$code = ‘’ ;
} else {
$para = ‘body’ ;
$link = ‘bodyLink’ ;
$head = ‘bodyHead’ ;
$code = ‘bodyCode’ ;
}
$pageInfo = ‘
$pageInfo = preg_replace(‘/\@\@/’,’
’, $pageInfo ) ;
$pageInfo = preg_replace(‘/WWW:([\w\s]+),URL:([^\s]+)\s/’, ‘$1 ‘,
$pageInfo ) ;
$pageInfo = preg_replace(‘/\%\%([\w\s,]+)\%\%/’, ‘
$1
’, $pageInfo )
;
$pageInfo = preg_replace(‘/\*\*/’, ‘
’, $pageInfo ) ;
$pageInfo = preg_replace (‘/\*\+/’,’
’, $pageInfo ) ;
$pageInfo = preg_replace(‘/\*\-/’,’
’, $pageInfo ) ;
return $pageInfo ;
}
Formatting text
Here are some formattiing examples:
1. %%This is a heading%%
2. Each paragraph is terminated by two at symbols, like this.@@
3. If a new line is all that’s needed, then two asterisks can
be used instead, and here’s how we also add a link:**
4. WWW:Personal Computer World,URL:www.pcw.co.uk will be
detected and turned into a link
For more articles on web development, click the tag below.
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
Old Street roundabout is being touted by the Government as the UK's answer to Silicon Valley, but it seems our best innovations are coming from all over the UK
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |