blob: ff387be10ebab1c4c8dfcdab704f35b29bcb8bf9 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2HTML Helper
3###########
4
5The HTML Helper file contains functions that assist in working with
6HTML.
7
Derek Jones0f0304c2013-07-21 11:35:30 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
18This helper is loaded using the following code::
19
20 $this->load->helper('html');
21
Derek Jones0f0304c2013-07-21 11:35:30 -070022Available Functions
23===================
24
Derek Jones8ede1a22011-10-05 13:34:52 -050025The following functions are available:
26
Derek Jones8ede1a22011-10-05 13:34:52 -050027
Derek Jones0f0304c2013-07-21 11:35:30 -070028.. function:: heading([$data = ''[, $h = '1'[, $attributes = '']]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +020029
30 :param string $data: Content
31 :param string $h: Heading level
32 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +020033 :returns: HTML heading tag
34 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +020035
Derek Jones0f0304c2013-07-21 11:35:30 -070036 Lets you create HTML heading tags. The first parameter will contain the
37 data, the second the size of the heading. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Derek Jones0f0304c2013-07-21 11:35:30 -070039 echo heading('Welcome!', 3);
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jones0f0304c2013-07-21 11:35:30 -070041 The above would produce: <h3>Welcome!</h3>
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Derek Jones0f0304c2013-07-21 11:35:30 -070043 Additionally, in order to add attributes to the heading tag such as HTML
44 classes, ids or inline styles, a third parameter is available::
Derek Jones8ede1a22011-10-05 13:34:52 -050045
Derek Jones0f0304c2013-07-21 11:35:30 -070046 echo heading('Welcome!', 3, 'class="pink"')
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Derek Jones0f0304c2013-07-21 11:35:30 -070048 The above code produces:
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Derek Jones0f0304c2013-07-21 11:35:30 -070050 .. code-block:: html
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jones0f0304c2013-07-21 11:35:30 -070052 <h3 class="pink">Welcome!<h3>
53
Derek Jones0f0304c2013-07-21 11:35:30 -070054.. function:: img([$src = ''[, $index_page = FALSE[, $attributes = '']]])
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Andrey Andreev53b8ef52012-11-08 21:38:53 +020056 :param string $src: Image source data
57 :param bool $index_page: Whether to treat $src as a routed URI string
58 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +020059 :returns: HTML image tag
60 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +020061
Derek Jones0f0304c2013-07-21 11:35:30 -070062 Lets you create HTML <img /> tags. The first parameter contains the
63 image source. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Derek Jones0f0304c2013-07-21 11:35:30 -070065 echo img('images/picture.jpg'); // gives <img src="http://site.com/images/picture.jpg" />
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Derek Jones0f0304c2013-07-21 11:35:30 -070067 There is an optional second parameter that is a TRUE/FALSE value that
68 specifics if the *src* should have the page specified by
69 ``$config['index_page']`` added to the address it creates.
70 Presumably, this would be if you were using a media controller::
Derek Jones8ede1a22011-10-05 13:34:52 -050071
Derek Jones0f0304c2013-07-21 11:35:30 -070072 echo img('images/picture.jpg', TRUE); // gives <img src="http://site.com/index.php/images/picture.jpg" alt="" />
73
74 Additionally, an associative array can be passed to the ``img()`` function
75 for complete control over all attributes and values. If an *alt* attribute
76 is not provided, CodeIgniter will generate an empty string.
77
78 Example::
79
80 $image_properties = array(
81 'src' => 'images/picture.jpg',
82 'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time',
83 'class' => 'post_images',
84 'width' => '200',
85 'height'=> '200',
86 'title' => 'That was quite a night',
87 'rel' => 'lightbox'
88 );
89
90 img($image_properties);
91 // <img src="http://site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a night" rel="lightbox" />
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreev3de130c2014-02-07 23:31:49 +020093.. function:: link_tag([$href = ''[, $rel = 'stylesheet'[, $type = 'text/css'[, $title = ''[, $media = ''[, $index_page = FALSE]]]]]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +020094
95 :param string $href: What are we linking to
96 :param string $rel: Relation type
97 :param string $type: Type of the related document
98 :param string $title: Link title
99 :param string $media: Media type
100 :param bool $index_page: Whether to treat $src as a routed URI string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200101 :returns: HTML link tag
102 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Derek Jones0f0304c2013-07-21 11:35:30 -0700104 Lets you create HTML <link /> tags. This is useful for stylesheet links,
105 as well as other links. The parameters are *href*, with optional *rel*,
106 *type*, *title*, *media* and *index_page*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Derek Jones0f0304c2013-07-21 11:35:30 -0700108 *index_page* is a boolean value that specifies if the *href* should have
109 the page specified by ``$config['index_page']`` added to the address it creates.
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Derek Jones0f0304c2013-07-21 11:35:30 -0700111 Example::
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200112
Derek Jones0f0304c2013-07-21 11:35:30 -0700113 echo link_tag('css/mystyles.css');
114 // gives <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
115
116 Further examples::
117
118 echo link_tag('favicon.ico', 'shortcut icon', 'image/ico');
119 // <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />
120
121 echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
122 // <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />
123
124 Additionally, an associative array can be passed to the ``link()`` function
125 for complete control over all attributes and values::
126
127 $link = array(
128 'href' => 'css/printer.css',
129 'rel' => 'stylesheet',
130 'type' => 'text/css',
131 'media' => 'print'
132 );
133
134 echo link_tag($link);
135 // <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Derek Jones0f0304c2013-07-21 11:35:30 -0700137
138.. function:: ul($list[, $attributes = ''])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200139
140 :param array $list: List entries
141 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200142 :returns: HTML-formatted unordered list
143 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200144
Derek Jones0f0304c2013-07-21 11:35:30 -0700145 Permits you to generate ordered or unordered HTML lists from simple or
146 multi-dimensional arrays. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Derek Jones0f0304c2013-07-21 11:35:30 -0700148 $list = array(
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200149 'red',
150 'blue',
Derek Jones0f0304c2013-07-21 11:35:30 -0700151 'green',
152 'yellow'
153 );
154
155 $attributes = array(
156 'class' => 'boldlist',
157 'id' => 'mylist'
158 );
159
160 echo ul($list, $attributes);
161
162 The above code will produce this:
163
164 .. code-block:: html
165
166 <ul class="boldlist" id="mylist">
167 <li>red</li>
168 <li>blue</li>
169 <li>green</li>
170 <li>yellow</li>
171 </ul>
172
173 Here is a more complex example, using a multi-dimensional array::
174
175 $attributes = array(
176 'class' => 'boldlist',
177 'id' => 'mylist'
178 );
179
180 $list = array(
181 'colors' => array(
182 'red',
183 'blue',
184 'green'
185 ),
186 'shapes' => array(
187 'round',
188 'square',
189 'circles' => array(
190 'ellipse',
191 'oval',
192 'sphere'
193 )
194 ),
195 'moods' => array(
196 'happy',
197 'upset' => array(
198 'defeated' => array(
199 'dejected',
200 'disheartened',
201 'depressed'
202 ),
203 'annoyed',
204 'cross',
205 'angry'
206 )
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200207 )
Derek Jones0f0304c2013-07-21 11:35:30 -0700208 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
Derek Jones0f0304c2013-07-21 11:35:30 -0700210 echo ul($list, $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Derek Jones0f0304c2013-07-21 11:35:30 -0700212 The above code will produce this:
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Derek Jones0f0304c2013-07-21 11:35:30 -0700214 .. code-block:: html
215
216 <ul class="boldlist" id="mylist">
217 <li>colors
218 <ul>
219 <li>red</li>
220 <li>blue</li>
221 <li>green</li>
222 </ul>
223 </li>
224 <li>shapes
225 <ul>
226 <li>round</li>
227 <li>suare</li>
228 <li>circles
229 <ul>
230 <li>elipse</li>
231 <li>oval</li>
232 <li>sphere</li>
233 </ul>
234 </li>
235 </ul>
236 </li>
237 <li>moods
238 <ul>
239 <li>happy</li>
240 <li>upset
241 <ul>
242 <li>defeated
243 <ul>
244 <li>dejected</li>
245 <li>disheartened</li>
246 <li>depressed</li>
247 </ul>
248 </li>
249 <li>annoyed</li>
250 <li>cross</li>
251 <li>angry</li>
252 </ul>
253 </li>
254 </ul>
255 </li>
256 </ul>
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
Derek Jonesb8c283a2013-07-19 16:02:53 -0700258.. function:: ol($list, $attributes = '')
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200259
260 :param array $list: List entries
261 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200262 :returns: HTML-formatted ordered list
263 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200264
Derek Jones0f0304c2013-07-21 11:35:30 -0700265 Identical to :func:`ul()`, only it produces the <ol> tag for
266 ordered lists instead of <ul>.
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200267
Derek Jones0f0304c2013-07-21 11:35:30 -0700268.. function:: meta([$name = ''[, $content = ''[, $type = 'name'[, $newline = "\n"]]]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200270 :param string $name: Meta name
271 :param string $content: Meta content
272 :param string $type: Meta type
273 :param string $newline: Newline character
Andrey Andreev3de130c2014-02-07 23:31:49 +0200274 :returns: HTML meta tag
275 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200276
Derek Jones0f0304c2013-07-21 11:35:30 -0700277 Helps you generate meta tags. You can pass strings to the function, or
278 simple arrays, or multidimensional ones.
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200279
Derek Jones0f0304c2013-07-21 11:35:30 -0700280 Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500281
Derek Jones0f0304c2013-07-21 11:35:30 -0700282 echo meta('description', 'My Great site');
283 // Generates: <meta name="description" content="My Great Site" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
Derek Jones0f0304c2013-07-21 11:35:30 -0700285 echo meta('Content-type', 'text/html; charset=utf-8', 'equiv');
286 // Note the third parameter. Can be "equiv" or "name"
287 // Generates: <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500288
Derek Jones0f0304c2013-07-21 11:35:30 -0700289 echo meta(array('name' => 'robots', 'content' => 'no-cache'));
290 // Generates: <meta name="robots" content="no-cache" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500291
Derek Jones0f0304c2013-07-21 11:35:30 -0700292 $meta = array(
293 array(
294 'name' => 'robots',
295 'content' => 'no-cache'
296 ),
297 array(
298 'name' => 'description',
299 'content' => 'My Great Site'
300 ),
301 array(
302 'name' => 'keywords',
303 'content' => 'love, passion, intrigue, deception'
304 ),
305 array(
306 'name' => 'robots',
307 'content' => 'no-cache'
308 ),
309 array(
310 'name' => 'Content-type',
311 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'
312 )
313 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
Derek Jones0f0304c2013-07-21 11:35:30 -0700315 echo meta($meta);
316 // Generates:
317 // <meta name="robots" content="no-cache" />
318 // <meta name="description" content="My Great Site" />
319 // <meta name="keywords" content="love, passion, intrigue, deception" />
320 // <meta name="robots" content="no-cache" />
321 // <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500322
Derek Jones8ede1a22011-10-05 13:34:52 -0500323
Derek Jones0f0304c2013-07-21 11:35:30 -0700324.. function:: doctype([$type = 'xhtml1-strict'])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200325
326 :param string $type: Doctype name
Andrey Andreev3de130c2014-02-07 23:31:49 +0200327 :returns: HTML DocType tag
328 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200329
Derek Jones0f0304c2013-07-21 11:35:30 -0700330 Helps you generate document type declarations, or DTD's. XHTML 1.0
331 Strict is used by default, but many doctypes are available.
Derek Jones8ede1a22011-10-05 13:34:52 -0500332
Derek Jones0f0304c2013-07-21 11:35:30 -0700333 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
Derek Jones0f0304c2013-07-21 11:35:30 -0700335 echo doctype(); // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Derek Jones8ede1a22011-10-05 13:34:52 -0500336
Derek Jones0f0304c2013-07-21 11:35:30 -0700337 echo doctype('html4-trans'); // <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Derek Jones8ede1a22011-10-05 13:34:52 -0500338
Derek Jones0f0304c2013-07-21 11:35:30 -0700339 The following is a list of doctype choices. These are configurable, and
340 pulled from application/config/doctypes.php
Derek Jones8ede1a22011-10-05 13:34:52 -0500341
Andrey Andreev3de130c2014-02-07 23:31:49 +0200342 =============================== =================== ==================================================================================================================================================
343 Document type Option Result
344 =============================== =================== ==================================================================================================================================================
345 XHTML 1.1 xhtml11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
346 XHTML 1.0 Strict xhtml1-strict <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
347 XHTML 1.0 Transitional xhtml1-trans <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
348 XHTML 1.0 Frameset xhtml1-frame <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
349 XHTML Basic 1.1 xhtml-basic11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
350 HTML 5 html5 <!DOCTYPE html>
351 HTML 4 Strict html4-strict <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
352 HTML 4 Transitional html4-trans <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
353 HTML 4 Frameset html4-frame <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
354 MathML 1.01 mathml1 <!DOCTYPE math SYSTEM "http://www.w3.org/Math/DTD/mathml1/mathml.dtd">
355 MathML 2.0 mathml2 <!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
356 SVG 1.0 svg10 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
357 SVG 1.1 Full svg11 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
358 SVG 1.1 Basic svg11-basic <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Basic//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd">
359 SVG 1.1 Tiny svg11-tiny <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
360 XHTML+MathML+SVG (XHTML host) xhtml-math-svg-xh <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
361 XHTML+MathML+SVG (SVG host) xhtml-math-svg-sh <!DOCTYPE svg:svg PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
362 XHTML+RDFa 1.0 xhtml-rdfa-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
363 XHTML+RDFa 1.1 xhtml-rdfa-2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.1//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd">
Andrey Andreev59f04262014-02-26 19:04:36 +0200364 =============================== =================== ==================================================================================================================================================
365
366.. function:: br([$count = 1])
367
368 :param int $count: Number of times to repeat the tag
369 :returns: HTML line break tag
370 :rtype: string
371
372 Generates line break tags (<br />) based on the number you submit.
373 Example::
374
375 echo br(3);
376
377 The above would produce:
378
379 .. code-block:: html
380
381 <br /><br /><br />
382
383 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
384 in combination with ``<br />`` instead.
385
386.. function:: nbs([$num = 1])
387
388 :param int $num: Number of space entities to produce
389 :returns: A sequence of non-breaking space HTML entities
390 :rtype: string
391
392 Generates non-breaking spaces (&nbsp;) based on the number you submit.
393 Example::
394
395 echo nbs(3);
396
397 The above would produce:
398
399 .. code-block:: html
400
401 &nbsp;&nbsp;&nbsp;
402
403 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
404 in combination with ``&nbsp;`` instead.