-
Notifications
You must be signed in to change notification settings - Fork 7.6k
definition list helper
Derek Jones edited this page Jul 5, 2012
·
6 revisions
This is an extension of the HTML_helper to include definition list
- capability.
The function dList() can be called with an array in the following format:
$list = array(
array('dt' => $result['user']),
array('dd' => array($result['role'],$result['email'])
))
which would produce:
<dl>
<dt>Brad</dt>
<dd>Grad Student</dd>
<dd>brad@codeigniter.com</dd>
</dl>
Also, if you would like to make a definition list at the end of a series of regular list items, you may stick the definition list array at the end of a series of ordered or unordered list arrays. This function will not allow any other lists to be nested within a definition list.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Definition List
*
* Generates an HTML definition list from an associative array. Use "dt"
* and "dd" as keys and set the value as an array.
*
* @author Bradford Mar
* @access public
* @param array
* @param mixed
* @return string
* @version 1.1
*/
if ( ! function_exists('dlist'))
{
function dlist($list, $attributes = '')
{
return _list('dl', $list, $attributes);
}
}
// ------------------------------------------------------------------------
/**
* Generates the list
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @access private
* @param string
* @param mixed
* @param mixed
* @param intiger
* @return string
*/
if ( ! function_exists('_list'))
{
function _list($type = 'ul', $list, $attributes = '', $depth = 0)
{
// If an array wasn't submitted there's nothing to do...
if ( ! is_array($list))
{
return $list;
}
// Set the indentation based on the depth
$out = str_repeat(" ", $depth);
// Were any attributes submitted? If so generate a string
if (is_array($attributes))
{
$atts = '';
foreach ($attributes as $key => $val)
{
$atts .= ' ' . $key . '="' . $val . '"';
}
$attributes = $atts;
}
//flag to substitue type with definition list
$dltype = array_key_exists('dt',$list[key($list)]);
// Write the opening list tag or replace with "dl"
$out .= "<". (($dltype) ? "dl" : $type).$attributes.">\n";
// Cycle through the list elements. If an array is
// encountered we will recursively call _list()
static $_last_list_item = '';
foreach ($list as $key => $val)
{
$_last_list_item = $key;
$out .= str_repeat(" ", $depth + 2);
//Don't bracket definition items with an <li>
if ( ! $dltype)
{
$out .= str_repeat(" ", $depth + 2);
$out .= "<li>";
}
if ( ! is_array($val))
{
$out .= $val;
}
//Definition list loop
elseif ($dltype)
{
foreach ($val as $dltag => $dlitems)
{
if (is_string($dlitems)) $dlitems = array($dlitems);
foreach($dlitems as $dlitem)
{
$out .= str_repeat(" ", $depth + 2)."<". $dltag .">".$dlitem."</". $dltag .">\n";
}
}
}
//Return to normal list loop
else
{
// prevents a scalar array from exposing its numbered position as list item text
if (is_string($_last_list_item)) $out .= $_last_list_item."\n";
$out .= _list($type, $val, '', $depth + 4);
$out .= str_repeat(" ", $depth + 2);
}
//No <li> brackets around definition items
if ( ! $dltype) $out .= "</li>\n";
}
// Set the indentation for the closing tag
$out .= str_repeat(" ", $depth);
// Write the closing list tag replace with dl if necessary
$out .= "</". (($dltype) ? "dl" : $type).">\n";
return $out;
}
}
/* End of file MY_html_helper.php */
/* Location: ./system/application/helpers/MY_html_helper.php */