Skip to content
Derek Jones edited this page Jul 5, 2012 · 8 revisions

Introduction

I noticed my view files get confusing because of all the little checks that have to be made when displaying values. For example

// data
array(
   array('start_date'=>'2008-01-01','total'=>'10'),
   array('start_date'=>'0000-00-00','total'=>'0')
);
// view
<?php foreach($rows as $row): ?>

<?php if($row['start_date'] != '0000-00-00') echo $row['start_date'] ?>
<?php if($row['total'] != '0') echo $row['start_date'] ?>

<?php endforeach ?>

This is why i created the display helper.

Code The code is split up in two parts. There are constants and there is the display_helper.php file

The constants are added to the config/constants.php files and make it easier for you to change the default value where ever you used it.

/*
| -----------------------------------------------------------------------
| Defaults
| -----------------------------------------------------------------------
*/
define('DEFAULT_DATETIME',                       '0000-00-00 00:00:00');
define('DEFAULT_DATETIME_NO_SEC',                '0000-00-00 00:00');
define('DEFAULT_DATE',                           '0000-00-00');
define('DEFAULT_TIME',                           '00:00:00');
define('DEFAULT_NUMBER',                         '0');

Add as many constants as you need.

The display_helper.php file contains the functions used in the view file

/* 
| ----------------------------
| show value if it's not the same as the default value
| ----------------------------
*/
function display($value,$default,$replacement = NULL)
{
    return ($value == $default)?(( ! is_null($replacement))?$replacement:''):$value;
}
/* 
| ----------------------------
| check if value is (not) the same as the default value
| ----------------------------
*/
function is_default($value,$default)
{
    return ($value == $default)?TRUE:FALSE;
}
/* 
| ----------------------------
| show value or a formatted string with the value in it when the value exists
| ----------------------------
*/
function display_isset($value,$str='')
{
   if( ! isset($value))
   {
       return '';
   }

   if($str != '')
   {
      return sprintf($str,$value);
   }

   return $value; 
}

It are only a few functions but they make view files a lot easier to read.

Usage The example view code with the helper is

<?php foreach($rows as $row): ?>

<?php echo display($row['start_date'],'0000-00-00') ?>
<?php echo display($row['total'],'0') ?>

<?php endforeach ?>
// or with the constants
<?php foreach($rows as $row): ?>

<?php echo display($row['start_date'],DEFAULT_DATE) ?>
<?php echo display($row['total'],DEFAULT_NUMBER) ?>

<?php endforeach ?>

An example of the display_isset function

// without helper
if(isset($class)): ' class="'.$class.'"'; endif ?>>hello world

// with helper echo display_isset($class,' class="%s"') ?>>hello world

Updates 29-08-2008 : added replacement parameter to display function. Usage ;

display($row['start_date'],DEFAULT_DATE,date('Y-m-d'))

Category:Contributions::Helpers::Miscallenous

Clone this wiki locally