PHP Classes

File: demo.php

Recommend this page to a friend!
  Classes of Nikola Posa   HeadSection   demo.php   Download  
File: demo.php
Role: Example script
Content type: text/plain
Description: Example script.
Class: HeadSection
Class for generating head section of an web page.
Author: By
Last change: New examples added.
Date: 14 years ago
Size: 2,516 bytes


Class file image Download
echo '<html>';


//You can pass some head section setup to constructor of HeadSection class.
$headSectionParams = array(
'title' => 'My site',
'meta' => array('name'=>'keywords', 'content'=>'test, generate, head'),
'css' => array('style.css', 'style/common.css'),
'js' => array('functions.js', 'js/test.js'),
'favicon' => 'path/to/favicon.ico'

//Creating new head section instance, passing array with options as first argument.
//In this example, we are setting contentType to null, and indent to 8 (8 whitespaces). That means that output of every head section element will be indented by 8 whitespaces.
//You can also set every of those options by calling their set methods, for example: $headSection->setIndent(4); or $headSection->setStartingTags(false);
$headSection = new HeadSection(array('contentType'=>null, 'indent'=>8), $headSectionParams);

$headSection->addMeta(array('name'=>'description', 'content'=>'Site for testing class which generates head section'), HeadSection::PREPEND); //Prepending new meta tag.

$headSection->addCss('form.css', HeadSection::PREPEND); //Prepending new CSS path to initialized array.

$headSection->addJs('test.js'); //Adding value to initialized JS array in default mode (APPEND).

$headSection->addTitle(' | Test'); //Appending value to title.

//Because HeadSection class redefines __toString() method, its object can be outputed:
echo $headSection;

//... or you can output head section by calling render() method: echo $headSection->render();

echo '

<body>Hello world!</body>


Page source will look something like this:

        <title>My site | Test</title>

        <meta name= "description" content = "Site for testing class which generates head section" />
        <meta name= "keywords" content = "test, generate, head" />

        <link href = "form.css" rel = "stylesheet" type = "text/css" />
        <link href = "style.css" rel = "stylesheet" type = "text/css" />
        <link href = "style/common.css" rel = "stylesheet" type = "text/css" />

        <link href = "path/to/favicon.ico" rel = "shortcut icon" type = "image/x-icon" />

        <script type = "text/javascript" src = "functions.js"></script>
        <script type = "text/javascript" src = "js/test.js"></script>
        <script type = "text/javascript" src = "test.js"></script>

<body>Hello world!</body>