Building blocks
Create site7 by copying site6.
- /cms
- ...
- site6
- site7
In this chapter, we are going to program the generation of a block which adds a banner to the site.
To test the result online, enter http://www.frasq.org/cms/site7 in the address bar of your navigator.
Create the folders blocks, buttons and logos in site7.
- /cms/site7
- blocks
- buttons
- logos
A block builds a modular part of the site. Contrary to an action, a block is never directly accessible via a URL.
Add the function build
to the file engine.php in the folder library:
- define('BLOCKS_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'blocks');
Specifies where all the blocks are stored. A block is a function defined in a file with the same name.
- function build($block) {
- $file = BLOCKS_DIR.DIRECTORY_SEPARATOR.$block.'.php';
- require_once $file;
- $func = basename($block);
- $args=func_get_args();
- array_shift($args);
- return call_user_func_array($func, $args);
- }
build
executes the function defined in the file designated by $block
. The rest of the arguments to build
is passed to the function. This function must return an HTML ou Javascript code which will be inserted in the output document, typically by an action or another block.
EXAMPLE: build('editing/editpage', $lang, $pagenum)
calls the function editpage
defined in the file editing/editpage.php with the arguments $lang
and $pagenum
.
Add a banner with a logo and a menu to the home page:
- function home($lang) {
- head('title', translate('home:title', $lang));
- $languages='home';
- $banner = build('banner', $lang, compact('languages'));
- $footer = view('footer', $lang);
- $content = view('home', $lang);
- $output = layout('standard', compact('banner', 'footer', 'content'));
- return $output;
- }
$banner
contains the result of the generation of the banner
block.
The block is placed by the view which organizes the layout of the final content:
- <body>
- <?php echo $banner; ?>
- <div id="content">
Add the files banner.php and languages.php in the folder blocks Add the view for the block directly in the folder views: Add the views for the languages block and the logo block in the folders views/en for the English version and views/fr for the French version. Copy the icons for the buttons in the folder buttons and the logo of the site in the folder logos:
Enter http://localhost/cms/site7 in the address bar of your navigator.
Click on a flag in the banner to switch the display in another language.
banner
builds the languages
block if languages
is in the component list of the banner defined by the parameter $components
.
Comments