Dynamic Pages

Summary

  • Dynamic functions on your pages are realized as so-called Phenotype Includes
  • All Includes (i.e. it’s source code, templates and usage options) can be edited within the Phenotype Backend (assumed config mode is enabled)
  • Includes can be embedded into Layouts (e.g. Navigation Elements) or inserted onto pages by Editors
  • Includes are always classes inheriting the PhenotypeIncludeStandard class
  • Includes can have their own (Smarty) templates.
  • Includes are a lot like controllers of MVC(Model View Controller)-architectures. Therefore Phenotype offers smartActions, an url based “magic” action method and templates selection, just like popular MVC-frameworks like Symfony and Zend Framework do
  • Includes can get cached in many different ways to minimize the load. So Phenotype helps your site to be dynamic as little as possible but as much as necessary

Creating new includes

Config → Pages → Includes → Create new include creates a new Include.

Following class scaffold is created for you:

<?php
/**
 * Name of your include
 *
 * @package phenotype
 * @subpackage application
 */
class PhenotypeInclude_1 extends PhenotypeInclude
{
 
  public $id = 1;
 
  public function display()
  {
    global $myDB, $myPage, $myRequest;
 
    // Initialize template access (=>$mySmarty) 
    // eval ($this->initRendering());    
  }
 
}

Basically, there isn’t much more necessary to know about Includes. When executing an Include (during Rendering of a Page) for every Include the method →display() get’s called. That’s the reason why you have to implement this method.

Templates or echo !?:

  • function display()

Within the display()-method you can just echo your html-code.

  • function initRendering()

If you want to access the templates of an include, just remove the comment slashes before the magic comand string eval ($this→initRendering());.
More details beneath.

Hello World include

Let’s do an “Hello World”-Example:

<?php 
/** 
 * Hello World
 *
 * @package phenotype
 * @subpackage application
 */ 
class PhenotypeInclude_1 extends PhenotypeInclude 
{ 
 
  public $id = 1; 
 
  function display() 
  { 
    echo "Hello World, current time: " . date("H:i") . PHP_EOL;    
  } 
 
}

Using (Smarty) Templates

As written above an include can have it’s own templates.

You manage templates of an include within the config mode (Config → Pages → Includes → “Hello World” → Config → Add Template). Every template needs an accessor variable. Default accessor variable is $TPL_1.

Assume you want to render “Hello World” using Smarty. Create $TPL_1 and rewrite the include. Initalize Template Engine as stated above:

<?php 
/** 
 * Hello World
 *
 * @package phenotype
 * @subpackage application
 */ 
class PhenotypeInclude_1 extends PhenotypeInclude 
{ 
 
  public $id = 1; 
 
  public function display() 
  { 
    // Initialize template access (=>$mySmarty)      
    eval ($this->initRendering());
 
    // Assign a value to template
    $mySmarty->assign("name","World");
 
    // Display template with template accessor $TPL_1
    $mySmarty->display($TPL_1);   
  } 
 
  public function getTime()
  {
      return date("H:i");
  }
 
}

And that’s the corresponding template:

Hello {$name|escape}, current time {$include->getTime()}


  • As you can see, the include object itself is accessible via the smarty template variable $include.
  • When using templates always remember to escape your output correctly, e.g. {$name|escape}
  • The smarty method →display(), displays a template, if you want to grab the output of an template use →fetch().
  • Smarty is a greate template engine, you can of course do much more with it, check http://smarty.php.net.

Embedding Includes / Caching

There are 4 possibilites to embed Includes

  1. within a layout
  2. inserted into a page with the “Include (Function)” page component
  3. before rendering of a page
  4. after rendering of a page


Option 3+4 are special cases and described later on this page.

Note: You must specify the usage possibilites of every include in the config mode. Otherwise it won’t be availabe for selection when editing pages and layouts.

Within a Layout

Embed an include into a layout by clicking Admin→Layout→Layout XY→Config→Add Include. You can decide wether the include get’s cached with the page or never. If it’s cached with the page, it’s executed only one once per page cache lifetime. That’s perfect for Navigation Elements.
If you have elements on your page, that must be calculated on every request (like date and time or a shopping basket) use the setting “never”.

Inserted with the “Include (Function)” page component

The “Include (Function)” page component is a component contained in both basic Phenotype Packages (PT_DEMO and PT_CORE). You should never delete this component, since it’s essential for building a dynamic site with Phenotype.

When placing an include with this page component, you have three caching options:

- with page
- never
- Request parameter hash

Options 1 and 2 have the same meanings as stated above for layouts.

Option 3 is special. It caches an include output depending on your request parameters. Assume you have page, which display a news article. This page is configured to be listen to urls like yourdomain/news/newsabc.

With Request parameter hash the include gets executed onyle once for every value of newsabc.
Note: It’s a short time cache valid for 60 minutes (defined by Phenotype::INCLUDE_CACHE_TIME), to avoid flooding your database.

Page Variables

Pages do have so called Page Variables. You can access them like the properties of content records or component with getters, e.g. $myPage→get(“category”).

This Page variables are edited on the script tab of a page (You need Admin rights). Complete path to Page Variables goes Editor→Pages→Script→Vars. These variables are inherative!! That means any value you enter for a given page, is spread to all pages underneath. Of course you can change them on subordinate pages. Page variables are helpful to configure branches of your site, possible values are categories, content for metatags, priority values for a Google Sitemap and so on.

Pre- and Post Includes

To embed an Include as Pre- or Post-Include you need at least Admin Rights. Edit a page like that Editor→Pages→PageXY→Script and you can determine an Include to be executed before or after Rendering of a page.

Possible Scenaries for that:
Pre: Access Restriction
Post: Regex Output manipulation or for example a TIDY HTML include.

Page Scripts

Well that’s finally the classic way of creating dynamic and interactive webpages. Every page can have a script, which may be executed up front. There are certain advantages compared to just putting a xyz.php into the web folder:

  • It’s cachable
  • You have all basic global objects on hand ($myPT, $myDB, $myRequest)
  • All scripts are clearly represented and accessible in config mode
  • You can turn a script offline by setting the page offline
  • You simply don’t have dozen of testxy.php files, that are usally forgotten and left on a webserver
  • You can even use the feature of pageversion, this means that a normal page could have a special version activated on special ocassions and that special version is just a cached plain coded script, no hazard at all.

Continue the introductory documentation with infos about helpers ...

You think Phenotype Wiki/Documentation could be better?
We too. Please contribute: Edit this page

Bookmark and Share