Layout and head of a document
Create site4 by copying site3.
- /cms
- ...
- site3
- site4
In this chapter, we are going to program the composition of a document and how the head
section is built.
To test the result online, enter http://www.frasq.org/cms/site4 in the address bar of your navigator. Display the source code of the document.
Create the folder layouts in site4.
- /cms/site4
- layouts
Split the content of the home page by saving the footer in the file footer.phtml and by keeping just the body of the central part in home.phtml.
- /cms/site4
- views
- en
- home.phtml
- footer.phtml
- fr
- home.phtml
- footer.phtml
- en
- views
- <div id="footer">
- <p>©2010-2011 frasq.org - All rights reserved - <a href="http://www.frasq.org">www.frasq.org</a></p>
- <p><img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16"/> <a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
- </div>
- <div id="footer">
- <p>©2010-2011 frasq.org - Tous droits réservés - <a href="http://www.frasq.org">www.frasq.org</a></p>
- <p><img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16" /> <a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
- </div>
NOTE: Using true characters instead of HTML entities for accents is now possible because the encoding type of the content of the page will be specified in the head section of the document. Remember to always save your files in UTF-8.
Reduce home.phtml in views/en and views/fr to the content of the central part of the home page:
- <h3>Welcome</h3>
- <h3>Bienvenue</h3>
Add the view which generates the head section of a document in the folder views. NOTE: This view doesn't depend on the language.
- /cms/site4
- views
- en
- fr
- head.phtml
- views
- <?php if (isset($lang)): ?>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="<?php echo $lang; ?>" />
- <?php else: ?>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <?php endif; ?>
- <?php if (false): ?>
- <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
- <?php endif; ?>
- <?php if (!empty($description)): ?>
- <meta name="description" content="<?php echo htmlspecialchars($description, ENT_NOQUOTES, 'UTF-8', false); ?>" />
- <?php endif; ?>
- <?php if (!empty($keywords)): ?>
- <meta name="keywords" content="<?php echo htmlspecialchars($keywords, ENT_NOQUOTES, 'UTF-8', false); ?>" />
- <?php endif; ?>
- <?php if (!empty($author)): ?>
- <meta name="author" content="<?php echo htmlspecialchars($author, ENT_NOQUOTES, 'UTF-8', false); ?>" />
- <?php endif; ?>
- <?php if (!empty($robots)): ?>
- <meta name="robots" content="<?php echo $robots; ?>" />
- <?php else: ?>
- <meta name="robots" content="index, follow" />
- <?php endif; ?>
- <?php if (true): ?>
- <link href="<?php echo $base_path; ?>/css/zero.css" rel="stylesheet" type="text/css" media="screen" />
- <?php endif; ?>
- <link href="<?php echo $base_path; ?>/css/screen.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="<?php echo $base_path; ?>/css/theme.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="<?php echo $base_path; ?>/css/print.css" rel="stylesheet" type="text/css" media="print" />
- <?php if (isset($stylesheets)): ?>
- <?php foreach ($stylesheets as $css) : ?>
- <link href="<?php echo $base_path; ?>/css/<?php echo $css['name']; ?>.css" rel="stylesheet" type="text/css" media="<?php echo $css['media']; ?>" />
- <?php endforeach; ?>
- <?php endif; ?>
- <?php if (isset($favicon)): ?>
- <link rel="shortcut icon" href="<?php echo $base_path; ?>/<?php echo $favicon; ?>.ico" type="image/x-icon" />
- <?php endif; ?>
- <?php if (isset($javascripts)): ?>
- <?php foreach ($javascripts as $js) : ?>
- <script type="text/javascript" src="<?php echo $base_path; ?>/js/<?php echo $js['name']; ?>.js"></script>
- <?php endforeach; ?>
- <?php endif; ?>
- <title><?php if (isset($title)) { echo htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8', false); } ?></title>
Add the file head.php in the folder library with the following content:
- /cms/site4
- library
- head.php
- library
- function head($type=false) {
- static $head = array();
- if (!$type) {
- return $head;
- }
- $args=func_get_args();
- array_shift($args);
- switch ($type) {
- case 'lang':
- $head['lang'] = $args[0];
- break;
- case 'title':
- $head['title'] = $args[0];
- break;
- case 'description':
- $head['description'] = $args[0];
- break;
- case 'favicon':
- $head['favicon'] = $args[0];
- break;
- case 'keywords':
- $head['keywords'] = $args[0];
- break;
- case 'author':
- $head['author'] = $args[0];
- break;
- case 'robots':
- $head['robots'] = $args[0];
- break;
- case 'stylesheet':
- $name=$args[0];
- $media=isset($args[1]) ? $args[1] : 'all';
- if (!isset($head['stylesheets'])) {
- $head['stylesheets'] = array(compact('name', 'media'));
- }
- else {
- foreach ($head['stylesheets'] as $css) {
- if ($css['name'] == $name) {
- break 2;
- }
- }
- $head['stylesheets'][]=compact('name', 'media');
- }
- break;
- case 'javascript':
- $name=$args[0];
- if (!isset($head['javascripts'])) {
- $head['javascripts'] = array(compact('name'));
- }
- else {
- foreach ($head['javascripts'] as $js) {
- if ($js['name'] == $name) {
- break 2;
- }
- }
- $head['javascripts'][]=compact('name');
- }
- break;
- default:
- return false;
- }
- return true;
- }
Add the file standard.phtml in the folder layouts with the following content:
- /cms/site4
- layouts
- standard.phtml
- layouts
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <?php echo $head; ?>
- </head>
- <body>
- <div id="content">
- <?php echo $content; ?>
- </div>
- <?php if (isset($footer)): ?>
- <?php echo $footer; ?>
- <?php endif; ?>
- </body>
- </html>
This view generates the final document. The global variable $head
defines the content of the head
section of the document. The global variables $content
and $footer
contains the body and the footer of the document. The footer is optional.
Modify how the document is build in the file engine.php in the folder library:
- require_once 'requesturi.php';
- require_once 'translate.php';
- require_once 'head.php';
- define('VIEWS_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'views');
- define('LAYOUTS_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'layouts');
Loads the of the head
function.
Defines the folder where all the files which generate the content of the document are grouped.
- $content=view($path, $lang);
- $footer=view('footer', $lang);
- head('lang', $lang);
- head('favicon', 'favicon');
- head('title', translate('home:title', $lang));
- $output=layout('standard', compact('content', 'footer'));
Builds the views of the body and the footer of the document.
Adds the language, the URL of the icon and the title of the document to the head
section.
Calls the function layout
with the layout model standard
, the content and the footer of the document.
- function layout($layout, $vars=false) {
- $head=view('head', false, head());
- if ($vars) {
- $vars['head'] = $head;
- }
- else {
- $vars = array('head' => $head);
- }
- $file = LAYOUTS_DIR.DIRECTORY_SEPARATOR.$layout.'.phtml';
- return render($file, $vars);
- }
layout
returns the content generated by the head
function and the contents of the array $vars
placed according to the model $layout
.
- function view($view, $lang=false, $vars=false) {
- $file = $lang ? VIEWS_DIR.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.$view.'.phtml' : VIEWS_DIR.DIRECTORY_SEPARATOR.$view.'.phtml';
- if (!file_exists($file)) {
- header('HTTP/1.0 404 Not Found');
- exit;
- }
- return render($file, $vars);
- }
view
passes $vars
to render
.
- function render($file, $vars=false) {
- global $base_path, $base_url, $base_root;
- if ($vars) {
- extract($vars);
- }
- ob_start();
- require $file;
- return ob_get_clean();
- }
render
places the variables $base_path
, $base_url
, $base_root
and all the variables defined by $vars
in the global context before loading the file $file
.
render
returns the document generated by $file
.
Enter http://localhost/cms/site4 in the addresse bar of your navigator.
Display the source code of the document. Check the result, in particular the head
section.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en" />
- <meta name="robots" content="index, follow" />
- <link href="/cms/site4/css/zero.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="/cms/site4/css/screen.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="/cms/site4/css/theme.css" rel="stylesheet" type="text/css" media="screen" />
- <link href="/cms/site4/css/print.css" rel="stylesheet" type="text/css" media="print" />
- <title>Home</title>
- </head>
- <body>
- <div id="content">
- <h3>Welcome</h3>
- </div>
- <div id="footer">
- <p>©2010 frasq.org - All rights reserved - <a href="http://www.frasq.org">www.frasq.org</a> - May 13th, 2010</p>
- <p><img src="/cms/site4/logos/ubuntu.png" alt="" width="16" height="16"/> <a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
- </div>
- </body>
- </html>
Comments