A basic news system
Before you continue you should have a good understanding of the following:
- HTML/XHTML
- PHP
- MySQL
- main settings
- articles settings
- display settings
Overview
To create a basic news system we should think about more things:
- we have to put some news into our database
- we should have an option to show all news together (news list)
- we should have an option to show one news article
- we'd like to show two latest news on the front page of website
After installation
Let's assume that you have successfully installed MyPortal CMS and you are ready to go on. First you have to set the main settings, where the only important thing is to set the start page. To get it work just set it to home (the name is arbitrary) and this is our first (front page) display. You can set the other main settings as you wish.
- click on Settings
- go to Main settings
- set session name and start page
Put some news into the DB
Next step is to go to the articles settings where we have to create a new articles category. We will name it News and set it's type to description and text. We don't need other fields for a basic news system, but you can agree that we could also add some image, datefield, author and other data to it.
- click to Articles settings
- type the name News, check description and select HTML option to text
Go to the Articles module and click the category News (that you've just created). At the bottom of the CMS just click on the NEW ARTICLE button.
- click the Articles module
- go to the News category
- click on the NEW ARTICLE button
After clicking the NEW ARTICLE button, the new article form will appear.
Type the name, description and text of you first article and leave the published checkbox checked. Finally click on the SAVE button to save the article. The first saved article will now appear at the top of the News category and you will see it's IID set to 1.
Now click on the NEW ARTICLE button again and add another article. You should add three or four articles into the News category before we can go on.
Create displays
Go to the display settings, where we should create two new displays: home and news. The home display will cover the front page of our website and will be set automatically because of the start page setting (http://localhost/ and http://localhost/home are the same displays!). The news display will activate when the user visits http://localhost/news.
- go to Settings and click Display settings
- click on the ADD DISPLAY button (modal window add main display will appear)
- type path home and click ADD button
Repeat this proccess again and add the news display as well.
Now you have general functions and two empty displays, which means that you could visit http://localhost/ and http://localhost/news (and nothing should happen). If the home or news display would not be created, the website would display the 404 error.
Create templates and sub-templates
First you have to create you main template.php, which holds the whole structure of your website. As you can see it is a simple XHTML page with one PHP line (echo $center). This is the center section of website to display other sub-templates. The number of sections is not limited and you could have also $head, $foot, $left, $right and other sections implemented in your custom XHTML code.
template.php
{{lang:html}}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>myFirstProject</title>
</head>
<body>
<div id="page">
<?=$center;?>
</div>
</body>
</html>
Our sub-templates will be tplnewslist.php, tplnewsmain.php and tpllast2_news.php, which will be injected into the $center section according to the URL address. So far we will have three possible options:
- http://localhost/
- http://localhost/news
- http://localhost/news/my-first-article
The first one shows our front page with last two news, the second one shows all news list and the third display shows one news article. The my-first-article address is only for example, because it is created from the name of your article. If your first article is Lorem ipsum, it's alias would be lorem-ipsum.
tpllast2news.php_
{{lang:php}}
<?foreach($last as $content){?>
<h1><?=$content['name'];?></h1>
<p><?=shorten($content['description'], 200);?></p>
<a href="<?=$MP['path'];?>/news/<?=$content['furl'];?>">more...</a>
<?}?>
<hr/>
<a href="<?=$MP['path'];?>/news">news archive</a>
This sub-template will be injected into the $center section when an user visits the front page of your website (http://localhost/). We will have to go to the display settings and add a new function to the home display to tell the MyPortal CMS to get last 2 news from the database and put them into the $last variable. The foreach loop shows the last two news with their name, description (only first 200 letters) and hyperlink. The news archive link is also added at the bottom.
tplnewslist.php
{{lang:php}}
<?foreach($news as $content){?>
<h1><?=$content['name'];?></h1>
<p><?=$content['description'];?></p>
<a href="<?=$MP['path'];?>/news/<?=$content['furl'];?>">more...</a>
<?}?>
<hr/>
<a href="<?=$MP['path'];?>/">home</a>
<a href="<?=$MP['path'];?>/news">news archive</a>
The tplnewslist.php sub-template is almost the same, we just renamed the news variable to $news, we left the description unchanged and we added the home hyperlink. Later we will add the articles list function to the news display in the MyPortal CMS.
tplnewsmain.php
{{lang:php}}
<h1><?=date('d.m.Y', strtotime($content['datefield']));?>, <?=$content['name'];?></h1>
<strong><?=$content['description'];?></strong>
<br/><br/>
<p><?=$content['text'];?></p>
<hr/>
<a href="<?=$MP['path'];?>/news">news archive</a>
The tplnewsmain.php sub-template show only one article with it's date, name, description and text. The news archive hyperlink is added at the bottom. We should add the articles article function to the news/* sub-display in the MyPortal CMS.
Add displays functions
Now we have to return to the display settings and add the articles list function to the home display. After clicking the add function icon the modal window display settings will appear.
You have to select articles module and set the articles list function. The function's output is set to last because we have the $last variable in the foreach loop in the tpllast2_news.php template. The whole PHP sub-template will be injected into the template.php to the center location. We select the News category, set the order rule to the datefield DESC and set limitation to 2 (just last two news).
Then we add another articles list function to the news display also.
Here we set the function's output to news (look at the tplnewslist.php into the foreach loop!), select the tplnewslist.php sub-template and remove the limitation, so we can get the list of all news.
Now is the time to add one sub-display to the news display.
Click to the add sub-display icon and the modal window will appear. We put only the asterisk sign (*) into the sub-display's name, because there could be any possible alias (unique for any news article).
Click to the add function icon on the news display to add a new function to the news/* sub-display.
In the case of news/* option we need the articles article function, which will get the article from the database and put it into the $content variable. We select the tplnewsmain.php sub-template and inject it into the center location (as all the other functions). The input setting is slightly different, because we have to set the GET option to 2 (the second element of the $GET array) and set the field to the furl. It means that MyPortal would get the article's alias (furl) from the URL address (second $GET) and search for it in the database.
The final step
That's it, everything works automatically from here on. The MyPortal functions provide all data from the database and put it into the selected variables. The use of variables in your PHP templates is arbitrary, you can do whatever you want with them. The design of the website doesn't have the affect to the PHP code, because the PHP code is simply written inside the XHTML code.