source: sourcecode/system/helpers/html_helper.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 8.6 KB
Line 
1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package             CodeIgniter
8 * @author              ExpressionEngine Dev Team
9 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license             http://codeigniter.com/user_guide/license.html
11 * @link                http://codeigniter.com
12 * @since               Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter HTML Helpers
20 *
21 * @package             CodeIgniter
22 * @subpackage  Helpers
23 * @category    Helpers
24 * @author              ExpressionEngine Dev Team
25 * @link                http://codeigniter.com/user_guide/helpers/html_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Heading
32 *
33 * Generates an HTML heading tag.  First param is the data.
34 * Second param is the size of the heading tag.
35 *
36 * @access      public
37 * @param       string
38 * @param       integer
39 * @return      string
40 */
41if ( ! function_exists('heading'))
42{
43        function heading($data = '', $h = '1', $attributes = '')
44        {
45                $attributes = ($attributes != '') ? ' '.$attributes : $attributes;
46                return "<h".$h.$attributes.">".$data."</h".$h.">";
47        }
48}
49
50// ------------------------------------------------------------------------
51
52/**
53 * Unordered List
54 *
55 * Generates an HTML unordered list from an single or multi-dimensional array.
56 *
57 * @access      public
58 * @param       array
59 * @param       mixed
60 * @return      string
61 */
62if ( ! function_exists('ul'))
63{
64        function ul($list, $attributes = '')
65        {
66                return _list('ul', $list, $attributes);
67        }
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Ordered List
74 *
75 * Generates an HTML ordered list from an single or multi-dimensional array.
76 *
77 * @access      public
78 * @param       array
79 * @param       mixed
80 * @return      string
81 */
82if ( ! function_exists('ol'))
83{
84        function ol($list, $attributes = '')
85        {
86                return _list('ol', $list, $attributes);
87        }
88}
89
90// ------------------------------------------------------------------------
91
92/**
93 * Generates the list
94 *
95 * Generates an HTML ordered list from an single or multi-dimensional array.
96 *
97 * @access      private
98 * @param       string
99 * @param       mixed
100 * @param       mixed
101 * @param       integer
102 * @return      string
103 */
104if ( ! function_exists('_list'))
105{
106        function _list($type = 'ul', $list, $attributes = '', $depth = 0)
107        {
108                // If an array wasn't submitted there's nothing to do...
109                if ( ! is_array($list))
110                {
111                        return $list;
112                }
113
114                // Set the indentation based on the depth
115                $out = str_repeat(" ", $depth);
116
117                // Were any attributes submitted?  If so generate a string
118                if (is_array($attributes))
119                {
120                        $atts = '';
121                        foreach ($attributes as $key => $val)
122                        {
123                                $atts .= ' ' . $key . '="' . $val . '"';
124                        }
125                        $attributes = $atts;
126                }
127                elseif (is_string($attributes) AND strlen($attributes) > 0)
128                {
129                        $attributes = ' '. $attributes;
130                }
131
132                // Write the opening list tag
133                $out .= "<".$type.$attributes.">\n";
134
135                // Cycle through the list elements.  If an array is
136                // encountered we will recursively call _list()
137
138                static $_last_list_item = '';
139                foreach ($list as $key => $val)
140                {
141                        $_last_list_item = $key;
142
143                        $out .= str_repeat(" ", $depth + 2);
144                        $out .= "<li>";
145
146                        if ( ! is_array($val))
147                        {
148                                $out .= $val;
149                        }
150                        else
151                        {
152                                $out .= $_last_list_item."\n";
153                                $out .= _list($type, $val, '', $depth + 4);
154                                $out .= str_repeat(" ", $depth + 2);
155                        }
156
157                        $out .= "</li>\n";
158                }
159
160                // Set the indentation for the closing tag
161                $out .= str_repeat(" ", $depth);
162
163                // Write the closing list tag
164                $out .= "</".$type.">\n";
165
166                return $out;
167        }
168}
169
170// ------------------------------------------------------------------------
171
172/**
173 * Generates HTML BR tags based on number supplied
174 *
175 * @access      public
176 * @param       integer
177 * @return      string
178 */
179if ( ! function_exists('br'))
180{
181        function br($num = 1)
182        {
183                return str_repeat("<br />", $num);
184        }
185}
186
187// ------------------------------------------------------------------------
188
189/**
190 * Image
191 *
192 * Generates an <img /> element
193 *
194 * @access      public
195 * @param       mixed
196 * @return      string
197 */
198if ( ! function_exists('img'))
199{
200        function img($src = '', $index_page = FALSE)
201        {
202                if ( ! is_array($src) )
203                {
204                        $src = array('src' => $src);
205                }
206
207                // If there is no alt attribute defined, set it to an empty string
208                if ( ! isset($src['alt']))
209                {
210                        $src['alt'] = '';
211                }
212
213                $img = '<img';
214
215                foreach ($src as $k=>$v)
216                {
217
218                        if ($k == 'src' AND strpos($v, '://') === FALSE)
219                        {
220                                $CI =& get_instance();
221
222                                if ($index_page === TRUE)
223                                {
224                                        $img .= ' src="'.$CI->config->site_url($v).'"';
225                                }
226                                else
227                                {
228                                        $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
229                                }
230                        }
231                        else
232                        {
233                                $img .= " $k=\"$v\"";
234                        }
235                }
236
237                $img .= '/>';
238
239                return $img;
240        }
241}
242
243// ------------------------------------------------------------------------
244
245/**
246 * Doctype
247 *
248 * Generates a page document type declaration
249 *
250 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
251 * html4-strict, html4-trans, and html4-frame.  Values are saved in the
252 * doctypes config file.
253 *
254 * @access      public
255 * @param       string  type    The doctype to be generated
256 * @return      string
257 */
258if ( ! function_exists('doctype'))
259{
260        function doctype($type = 'xhtml1-strict')
261        {
262                global $_doctypes;
263
264                if ( ! is_array($_doctypes))
265                {
266                        if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
267                        {
268                                include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
269                        }
270                        elseif (is_file(APPPATH.'config/doctypes.php'))
271                        {
272                                include(APPPATH.'config/doctypes.php');
273                        }
274
275                        if ( ! is_array($_doctypes))
276                        {
277                                return FALSE;
278                        }
279                }
280
281                if (isset($_doctypes[$type]))
282                {
283                        return $_doctypes[$type];
284                }
285                else
286                {
287                        return FALSE;
288                }
289        }
290}
291
292// ------------------------------------------------------------------------
293
294/**
295 * Link
296 *
297 * Generates link to a CSS file
298 *
299 * @access      public
300 * @param       mixed   stylesheet hrefs or an array
301 * @param       string  rel
302 * @param       string  type
303 * @param       string  title
304 * @param       string  media
305 * @param       boolean should index_page be added to the css path
306 * @return      string
307 */
308if ( ! function_exists('link_tag'))
309{
310        function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
311        {
312                $CI =& get_instance();
313
314                $link = '<link ';
315
316                if (is_array($href))
317                {
318                        foreach ($href as $k=>$v)
319                        {
320                                if ($k == 'href' AND strpos($v, '://') === FALSE)
321                                {
322                                        if ($index_page === TRUE)
323                                        {
324                                                $link .= 'href="'.$CI->config->site_url($v).'" ';
325                                        }
326                                        else
327                                        {
328                                                $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
329                                        }
330                                }
331                                else
332                                {
333                                        $link .= "$k=\"$v\" ";
334                                }
335                        }
336
337                        $link .= "/>";
338                }
339                else
340                {
341                        if ( strpos($href, '://') !== FALSE)
342                        {
343                                $link .= 'href="'.$href.'" ';
344                        }
345                        elseif ($index_page === TRUE)
346                        {
347                                $link .= 'href="'.$CI->config->site_url($href).'" ';
348                        }
349                        else
350                        {
351                                $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
352                        }
353
354                        $link .= 'rel="'.$rel.'" type="'.$type.'" ';
355
356                        if ($media      != '')
357                        {
358                                $link .= 'media="'.$media.'" ';
359                        }
360
361                        if ($title      != '')
362                        {
363                                $link .= 'title="'.$title.'" ';
364                        }
365
366                        $link .= '/>';
367                }
368
369
370                return $link;
371        }
372}
373
374// ------------------------------------------------------------------------
375
376/**
377 * Generates meta tags from an array of key/values
378 *
379 * @access      public
380 * @param       array
381 * @return      string
382 */
383if ( ! function_exists('meta'))
384{
385        function meta($name = '', $content = '', $type = 'name', $newline = "\n")
386        {
387                // Since we allow the data to be passes as a string, a simple array
388                // or a multidimensional one, we need to do a little prepping.
389                if ( ! is_array($name))
390                {
391                        $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
392                }
393                else
394                {
395                        // Turn single array into multidimensional
396                        if (isset($name['name']))
397                        {
398                                $name = array($name);
399                        }
400                }
401
402                $str = '';
403                foreach ($name as $meta)
404                {
405                        $type           = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
406                        $name           = ( ! isset($meta['name']))             ? ''    : $meta['name'];
407                        $content        = ( ! isset($meta['content']))  ? ''    : $meta['content'];
408                        $newline        = ( ! isset($meta['newline']))  ? "\n"  : $meta['newline'];
409
410                        $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
411                }
412
413                return $str;
414        }
415}
416
417// ------------------------------------------------------------------------
418
419/**
420 * Generates non-breaking space entities based on number supplied
421 *
422 * @access      public
423 * @param       integer
424 * @return      string
425 */
426if ( ! function_exists('nbs'))
427{
428        function nbs($num = 1)
429        {
430                return str_repeat("&nbsp;", $num);
431        }
432}
433
434
435/* End of file html_helper.php */
436/* Location: ./system/helpers/html_helper.php */
Note: See TracBrowser for help on using the repository browser.