html_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter HTML Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/html_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Heading
00032  *
00033  * Generates an HTML heading tag.  First param is the data.
00034  * Second param is the size of the heading tag.
00035  *
00036  * @access      public
00037  * @param       string
00038  * @param       integer
00039  * @return      string
00040  */     
00041 if ( ! function_exists('heading'))
00042 {
00043         function heading($data = '', $h = '1')
00044         {
00045                 return "<h".$h.">".$data."</h".$h.">";
00046         }
00047 }
00048 
00049 // ------------------------------------------------------------------------
00050 
00051 /**
00052  * Unordered List
00053  *
00054  * Generates an HTML unordered list from an single or multi-dimensional array.
00055  *
00056  * @access      public
00057  * @param       array
00058  * @param       mixed
00059  * @return      string
00060  */     
00061 if ( ! function_exists('ul'))
00062 {
00063         function ul($list, $attributes = '')
00064         {
00065                 return _list('ul', $list, $attributes);
00066         }
00067 }
00068 
00069 // ------------------------------------------------------------------------
00070 
00071 /**
00072  * Ordered List
00073  *
00074  * Generates an HTML ordered list from an single or multi-dimensional array.
00075  *
00076  * @access      public
00077  * @param       array
00078  * @param       mixed
00079  * @return      string
00080  */     
00081 if ( ! function_exists('ol'))
00082 {
00083         function ol($list, $attributes = '')
00084         {
00085                 return _list('ol', $list, $attributes);
00086         }
00087 }
00088 
00089 // ------------------------------------------------------------------------
00090 
00091 /**
00092  * Generates the list
00093  *
00094  * Generates an HTML ordered list from an single or multi-dimensional array.
00095  *
00096  * @access      private
00097  * @param       string
00098  * @param       mixed           
00099  * @param       mixed           
00100  * @param       intiger         
00101  * @return      string
00102  */     
00103 if ( ! function_exists('_list'))
00104 {
00105         function _list($type = 'ul', $list, $attributes = '', $depth = 0)
00106         {
00107                 // If an array wasn't submitted there's nothing to do...
00108                 if ( ! is_array($list))
00109                 {
00110                         return $list;
00111                 }
00112         
00113                 // Set the indentation based on the depth
00114                 $out = str_repeat(" ", $depth);
00115         
00116                 // Were any attributes submitted?  If so generate a string
00117                 if (is_array($attributes))
00118                 {
00119                         $atts = '';
00120                         foreach ($attributes as $key => $val)
00121                         {
00122                                 $atts .= ' ' . $key . '="' . $val . '"';
00123                         }
00124                         $attributes = $atts;
00125                 }
00126         
00127                 // Write the opening list tag
00128                 $out .= "<".$type.$attributes.">\n";
00129 
00130                 // Cycle through the list elements.  If an array is 
00131                 // encountered we will recursively call _list()
00132 
00133                 static $_last_list_item = '';
00134                 foreach ($list as $key => $val)
00135                 {       
00136                         $_last_list_item = $key;
00137 
00138                         $out .= str_repeat(" ", $depth + 2);
00139                         $out .= "<li>";
00140                 
00141                         if ( ! is_array($val))
00142                         {
00143                                 $out .= $val;
00144                         }
00145                         else
00146                         {
00147                                 $out .= $_last_list_item."\n";
00148                                 $out .= _list($type, $val, '', $depth + 4);
00149                                 $out .= str_repeat(" ", $depth + 2);
00150                         }
00151 
00152                         $out .= "</li>\n";              
00153                 }
00154 
00155                 // Set the indentation for the closing tag
00156                 $out .= str_repeat(" ", $depth);
00157         
00158                 // Write the closing list tag
00159                 $out .= "</".$type.">\n";
00160 
00161                 return $out;
00162         }
00163 }
00164         
00165 // ------------------------------------------------------------------------
00166 
00167 /**
00168  * Generates HTML BR tags based on number supplied
00169  *
00170  * @access      public
00171  * @param       integer
00172  * @return      string
00173  */     
00174 if ( ! function_exists('br'))
00175 {
00176         function br($num = 1)
00177         {
00178                 return str_repeat("<br />", $num);
00179         }
00180 }
00181         
00182 // ------------------------------------------------------------------------
00183 
00184 /**
00185  * Image
00186  *
00187  * Generates an <img /> element
00188  *
00189  * @access      public
00190  * @param       mixed
00191  * @return      string
00192  */     
00193 if ( ! function_exists('img'))
00194 {
00195         function img($src = '', $index_page = FALSE)
00196         {
00197                 if ( ! is_array($src) )
00198                 {
00199                         $src = array('src' => $src);
00200                 }
00201 
00202                 $img = '<img';
00203                 
00204                 foreach ($src as $k=>$v)
00205                 {
00206 
00207                         if ($k == 'src' AND strpos($v, '://') === FALSE)
00208                         {
00209                                 $CI =& get_instance();
00210 
00211                                 if ($index_page === TRUE)
00212                                 {
00213                                         $img .= ' src="'.$CI->config->site_url($v).'" ';
00214                                 }
00215                                 else
00216                                 {
00217                                         $img .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';
00218                                 }
00219                         }
00220                         else
00221                         {
00222                                 $img .= " $k=\"$v\" ";
00223                         }
00224                 }
00225 
00226                 $img .= '/>';
00227 
00228                 return $img;
00229         }
00230 }
00231 
00232 // ------------------------------------------------------------------------
00233 
00234 /**
00235  * Link
00236  *
00237  * Generates link to a CSS file
00238  *
00239  * @access      public
00240  * @param       mixed   stylesheet hrefs or an array
00241  * @param       string  rel
00242  * @param       string  type
00243  * @param       string  title
00244  * @param       string  media
00245  * @param       boolean should index_page be added to the css path 
00246  * @return      string
00247  */     
00248 if ( ! function_exists('link_tag'))
00249 {
00250         function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
00251         {
00252                 $CI =& get_instance();
00253 
00254                 $link = '<link ';
00255 
00256                 if (is_array($href))
00257                 {
00258                         foreach ($href as $k=>$v)
00259                         {
00260                                 if ($k == 'href' AND strpos($v, '://') === FALSE)
00261                                 {
00262                                         if ($index_page === TRUE)
00263                                         {
00264                                                 $link .= ' href="'.$CI->config->site_url($v).'" ';
00265                                         }
00266                                         else
00267                                         {
00268                                                 $link .= ' href="'.$CI->config->slash_item('base_url').$v.'" ';
00269                                         }
00270                                 }
00271                                 else
00272                                 {
00273                                         $link .= "$k=\"$v\" ";
00274                                 }
00275                         }
00276                         
00277                         $link .= "/>\n";
00278                 }
00279                 else
00280                 {
00281                         if ( strpos($href, '://') !== FALSE)
00282                         {
00283                                 $link .= ' href="'.$href.'" ';
00284                         }
00285                         elseif ($index_page === TRUE)
00286                         {
00287                                 $link .= ' href="'.$CI->config->site_url($href).'" ';
00288                         }
00289                         else
00290                         {
00291                                 $link .= ' href="'.$CI->config->slash_item('base_url').$href.'" ';
00292                         }
00293                                 
00294                         $link .= 'rel="'.$rel.'" type="'.$type.'" ';
00295                         
00296                         if ($media      != '')
00297                         {
00298                                 $link .= 'media="'.$media.'" ';
00299                         }
00300 
00301                         if ($title      != '')
00302                         {
00303                                 $link .= 'title="'.$title.'" ';
00304                         }
00305                         
00306                         $link .= '/>'."\n";
00307                 }
00308 
00309         
00310                 return $link;
00311         }
00312 }
00313 
00314 // ------------------------------------------------------------------------
00315 
00316 /**
00317  * Generates meta tags from an array of key/values
00318  *
00319  * @access      public
00320  * @param       array
00321  * @return      string
00322  */     
00323 if ( ! function_exists('meta'))
00324 {
00325         function meta($meta = array(), $newline = "\n")
00326         {
00327                 $str = '';
00328                 foreach ($meta as $key => $val)
00329                 {
00330                         $str .= '<meta http-equiv="'.$key.'" content="'.$val.'" />'.$newline;
00331                 }
00332 
00333                 return $str;
00334         }
00335 }
00336 
00337 // ------------------------------------------------------------------------
00338 
00339 /**
00340  * Generates non-breaking space entities based on number supplied
00341  *
00342  * @access      public
00343  * @param       integer
00344  * @return      string
00345  */     
00346 if ( ! function_exists('nbs'))
00347 {
00348         function nbs($num = 1)
00349         {
00350                 return str_repeat("&nbsp;", $num);
00351         }
00352 }
00353 
00354 
00355 /* End of file html_helper.php */
00356 /* Location: ./system/helpers/html_helper.php */