Creating a new Page Template

From PLICITY Online Documentation

Jump to: navigation, search

1. Create a Template Directory.
Go to the content/themes/default/page path and create a new folder. The name of the folder you create will automatically be the Template Key of the template you are about to create. It has to be a valid UNIX name. In this tutorial we will name the new folder new_template.

2. Create a PHP file named template.php in the template's directory.
This file is going to contain the actual main template code of the page.

3. Provide a Template Name in the header of the newly created PHP file.

A valid header looks like the following:

<?php
///////////////////////////////////////////////////////////////////////////////
//
// Template Name: My New Template
// Template Key: new_template
//
///////////////////////////////////////////////////////////////////////////////

The Template Name should be chosen as the human-readable version of the Template Key. However, there is no rule that forbids Template Names that are completely different from their corresponding Template Keys. Both Template Names and Template Keys can be used by the system to uniquely identify a template. Specifying a Template Key in the header of a template.php file is optional. If no key is specified explicitly, the directory name of the template is used. Template Keys must always be valid UNIX names.

4. Provide a Slot Structure.
The slot structure contains the description of the content slots and content fragments. A content slot is a stack of zero or more fragments. Content slots are identified by their position (column and row) and name. A simple slot structure could look like the following:

//- BEGIN SLOTS
if($export_slots)
{
    $slots[0][0] = array( "Content",
                          array("name" => "Main Text",
                                "type" => "text")
                        );
    $slots[1][0] = array( "Sidebar"
                          array("name" => "Sidebar Text",
                                "type" => "text")
                        );
}
//- END SLOTS

As you can see, the slot structure is described in a multi-dimensional associative array named $slots. The first index describes the slot's column and the second index describes the slot row. That is you could think of it as $slots[$x][$y]. Each position (column/row combination) is again an array, which stores the definition of a slot. The first element of that array must be the name of the slot and all subsequent elements define the fragments within the slot.

The $slots array is nested in an if statement, which is used for performance optimization.

5. Provide template code.

//- BEGIN TEMPLATE OUTPUT
if(!$supress_output) {

// Template code starts here.
///////////////////////////////////////////////////////////////////////////////
?>

<body>
    <h1><?php the_title(); ?></h1>
    <table border="0" cellpadding="0" cellspacing="0" summary="" width="100%">
        <tr>
            <td width="80%">
                <div style="font-weight: bold;">Our page content:</div>
                <div><?php the_slot("Content"); ?></div>
            </td>
            <td width="20%">
                <div style="font-weight: bold;">Our sidebar:</div>
                <div><?php the_slot("Sidebar"); ?></div>
            </td>
        </tr>
    </table>
</body>

<?php
}
//- END TEMPLATE OUTPUT
?>

The sample code above represents a very simple template, which displays the page's main content in a wide text area and shows a sidebar on the right side of a page.

As you can see, the template code itself consists of HTML markup mixed with PHP code. There are some important things you need to know about template code in PLICITY:

  • Template HTML markup usually starts with the <body> element and ends with the </body> element. The HTML header and footer are defined as environmental templates. For pages PLICITY combines header, template code and footer for generating pages.
  • You can call all built-in core PLICITY functions from PHP code inside the template. For basic template functions, there is a special Template API, which provides simple methods that are often required in template building.
Personal tools