====== Step2. Displaying newsline using WACT-template ======
===== Creating database table for news and filling it with data =====
Create an empty database according to settings in **~crud/settings/db.conf.php** file if not yet. Our project requires just a single table "news". Here is SQL schema for "news" table:
CREATE TABLE `news` (
`id` bigint(20) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`annotation` text NOT NULL,
`content` longtext NOT NULL,
`date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`id`),
KEY `date` (`date`)
) TYPE=MyISAM;
Now let's fill "news" table with some initial data:
insert into `news` values (3,'We finished model part','Model is finished.','Model is finished. And more text here!','2006-08-15');
insert into `news` values (4,'We finished view partfds','All templates are ready!','We created display.html, detail_display.html, create.html, edit.html, admin_display.html','2006-08-16');
insert into `news` values (5,'Controller is finished','Controller is ready now!','The most difficult part was to describe controller!','2006-08-17');
insert into `news` values (6,'Basic tutorial is finished and ready to be published','All pages are ready as well as php-code. Now it\'s time to rock\'n\'roll!','','2006-08-18');
You can find this sql code in archive of crud demo application that can be downloaded from [[http://projects.limb-project.com]]. For MySQL use init/db.mysql, for SQLite use init/db.sqlite
===== Creating Model class being "news" table gateway =====
WEB_APP package follows MVC (Model-View-Controller) pattern. It means that our application can be logically divided into three separate components:
* Model (stores base and handles business logic),
* View (renders presentation logic) and
* Controller (controls application flow).
First we need a Model for our application. Let's create **~crud/src/model/** folder and place **News.class.php** in it with the following contents:
lmbActiveRecord class follows ActiveRecord pattern which means that there is a class encapsulating business logic, database access and acting as a data container at the same time.
lmbActiveRecord class can be found in [[limb3_2007_4:en:packages:active_record|ACTIVE_RECORD package]]. Limb3 implementation of ActiveRecord pattern follows in many points Ruby-on-Rails ActiveRecord implementation.
News class automatically determines that it should store data in "news" table. Actually lmbActiveRecord tries to guess the table name by converting CamelCased class name to under_scored variant if it's not specified explicitly.
===== Creating Controller class for news =====
The term **controller** can be interpreted in different ways, but in our case controller is a class that "knows" what to do when a user requests some specific functionality, e.g. requests a newsline to be displayed.
So, we need a controller for our newsline. Let's create **~crud/src/controller/** folder and place **NewsController.class.php** file in it with simple php-code:
NewController is just inherits from lmbController and we don't need anything else in our controller just to display newsline.
===== Creating template to display newsline =====
Now it's time for the View part of our project. Generally with Limb3 you don't need to create any View classes - as you will see WACT templates are pretty powerful enough to avoid any additional classes.
Create a folder **~crud/template/news/** for templates and put **display.html** file with the following contents:
Newsline
Newsline.
It's time to check if we are doing everything right. Type http://%tutorial_address%/news in your browser and you should see a simple page with Newsline title on it.
Now we will display the newsline. To do so we have to modify our ~crud/template/news/display.html (next time we will use a shortcut of path, like news/display.html) template as follows:
Newsline
Newsline.
| ID |
Date |
Title |
| {$id} |
{$date} |
{$title} |
|
{$annotation}
|
Limb3 uses WACT template engine by default.
The main elements of WACT templates are [[limb3_2007_4:en:packages:wact:tags|tags]] and [[limb3_2007_4:en:packages:wact:expressions|output expressions]]. Let's briefly explain what elements of the template engine were used here:
* [[limb3_2007_4:ru:packages:wact:tags:lmb_fetch_tags:lmb_active_record_fetch_tag|]] tag is used to fetch data from class specified by **using** attribute (this is our News class) and to transfer fetched data to the component named by **target** attribute. In our case, with id news will get all the fetched data.
* [[limb3_2007_4:ru:packages:wact:tags:list_tags:list_list_tag|]] uses to output lists and tables. The part that lies within tag is repeated for each record.
* With several output expression like {$date}, {$title}, {$annotation} we display values of each news.
As a result, you should see at http://%tutorial_address%/news something like this:
{{{:limb3_2007_4:ru:tutorials:basic:simple_list.png|:limb3_2007_4:ru:tutorials:basic:simple_list.png}}}
WACT also supports raw php-blocks in templates so we can skip usage and retrieve data with php:
[...]
Newsline.
[...]