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