Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by zdknudsen )

Display helper

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): ?>
<tr>
<
td><?php if($row['start_date'!= '0000-00-00') echo $row['start_date'] ?></td>
<
td><?php if($row['total'!= '0') echo $row['start_date'] ?></td>
</
tr>
<?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): ?>
<tr>
<
td><?php echo display($row['start_date'],'0000-00-00'?></td>
<
td><?php echo display($row['total'],'0'?></td>
</
tr>
<?php endforeach ?>
// or with the constants
<?php foreach($rows as $row): ?>
<tr>
<
td><?php echo display($row['start_date'],DEFAULT_DATE?></td>
<
td><?php echo display($row['total'],DEFAULT_NUMBER?></td>
</
tr>
<?php endforeach ?> 

An example of the display_isset function

// without helper
<p<?php if(isset($class)): ' class="'.$class.'"'; endif ?>>hello world</p>
// with helper
<p<?php echo display_isset($class,' class="%s"'?>>hello world</p

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