Book Image

concrete5 Beginner's Guide

Book Image

concrete5 Beginner's Guide

Overview of this book

Table of Contents (19 chapters)
concrete5
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – accessing attribute data from a template


Carry out the following steps:

  1. Open header.php of your theme in your editor.

  2. Remove the <body> tag; we're going to replace it with some code, which includes the background picture.

  3. Insert the following code right where you've removed the <body> tag:

    <?php
    $backgroundAttribute = $c->getAttribute('background');
    if ($backgroundAttribute) {
     $backgroundFile = $backgroundAttribute->getRelativePath();
     echo "<body style=\"background:url('{$backgroundFile}')\">";
    }
    else {
    echo "<body>";
    }
    ?>
  4. Reload your page and you'll see the new background picture instead of the color gray.

What just happened?

We removed the static body tag and inserted some PHP code to fetch the attribute value. This works by using $c->getAttribute. $c is a global variable referring to the current page.

getAttribute is a method available on all collection objects like pages. This method works for all attributes but depending on the attribute...