The platform | Features | Roadmap | Download | Licensing |EnglishItalian

SourceForge.net Logo

Support Wedgefish development

Some template programming examples

The following are some practical examples showing how you can develop dynamic template on the Wedgefish platform.

Example 1: Conditional HTML sections

Imagine you've placed the following HTML code in the view file associated to a page URL, to display a warning message only on certain conditions:

<div id="WOS_warning">$message</div>

The controller file can determine if this object has to be displayed if certain conditions are met, or hide it otherwise:

if(!wos_is_logged_in())
  wos_view_add_element('warning', array(
    'message' => 'Please login to send your comments'
  ));

Resulting output, if user has not logged in, will be the following:

Please login to send your comments

Should this message be formatted in a particular way, we'll add a class attribute to the div tag, or alternatively, assumed we used as id "wos_warning", set up a CSS class named #warning in the cascading style sheet linked to our page.

Example 2: Looping HTML

Imagine you wish to display a list of product categories in an e-commerce website, as an ordered list.
We'll then add the following HTML code in the view file associated to our page URL, in the desired position:

<ol>
  <li id="WOS_category"><a href="$url">$title</a>
</ol>

Now we'll have to add some code to our page controller file, to retrieve from the WODBMS the list of categories and output the <li> tag as many times as needed, for each category to be displayed:

/*
Get list of categories from the WODBMS, sorted by title, excluding hidden ones, and return, for each category, its associated page URL and its title, already localized in the currently selected language or in the browser default language.
Note: This example assumes that a "Categories" class has been defined in the WODBMS of the current website, with appropriate fields.
*/
$categories = wos_query_items('Categories', 'wos_url,title', 'wos_visible', 2);

// For each category, repeat element marked with id="WOS_category"
foreach($categories as $item_id => $fields) {
  wos_view_add_element('category', array(
    'url' => wos_get_relative_url($fields['wos_url']),
    'title' => $fields['title']
  ));
}

This technique may be implemented whichever the data to be retrieved and the layout to display them. With a few lines of code it is then possible to completely separate PHP code from HTML formatting, producing clean and easy to maintain code.

Example 3: Nested objects with conditional and looping HTML

We'll now introduce a more complex example, showing in full the potential of Wedgefish templating system.
Often, you'll want to display a section of a page only if there is actually data to be displayed within it.
For example, we could have a panel listing special offers which should be hidden if there are no promotions currently in progress.
We'll then add a code like this in our view file:

<div id="WOS_promotions">
  <p>This is a list of current promotions:</p>
  <ul>
    <li id="WOS_product"><a href="$url">$title</a>$title</li>
  </ul>
</div>

Now, we'll edit our controller file to ensure the object marked with id "WOS_promotions" is only displayed if there are products in promotion at the moment, and in this case, repeating the object marked with id "WOS_product" for each product to be displayed:

/*
Read the list of products currently in promotion from the WODBMS, sorted as specified in the "Promotions" container object, and, for each product, return its URL and title localized in the currently selected language or in the browser default language.
Note: This example assumes that a class named "Product groups", to be used to group products, has been defined in the WODBMS of the current site, and that its objects contain a multiple indexed "product" field, related with the class "Products" (which also has to be defined in the WODBMS).
For this example to work, there should exist an object, belonging to class "product groups", named "Promotions" (in English language), to be used to group products you want to put in promotion, in a particular order.
*/
$promotions = wos_query_item('Product groups', 'product(wos_url),product(title)', wos_query_condition('title=%s', 'Promotions'));

// Store list of URL of products currently in promotion in an array
$url = wos_array_slice($menu, 'product*(wos_url)');
// Store list of localized titles of products currently in promotion in an array
$titles = wos_array_slice($menu, 'product*(title)');

// Only if there's at least a product in promotion, display HTML object with id "WOS_promotions"
if(!empty($url)) {
  $promotions =& wos_view_add_element('promotions');
  foreach($url as $index => $u) {
    $promotions->addElement('product', array(
      'url' => wos_get_relative_url($u),
      'title' => $titles[$index]
    ));
  }
}

These are only some examples showing how to develop code to generate highly dynamic server-side output by using Wedgefish templating system.
Others will soon follow.
Note: By using functions contained in "models" provided by certain modules (such as "comments"), HTML objects in view files can be automatically populated by simply passing them some parameters. This topic will be further discussed in the "modules" section.

 

Wedgefish - Copyright © 2005-2008 by Massimiliano Alessandri
This is free software, and you may redistribute it under the Affero GPL.
Wedgefish comes with absolutely no warranty; for details, see the license.
You may download the currently running source code for this software.