blob: 955ffefc57acbd1b86db35c1554d5dc0d88e8396 [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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020028.. php:function:: heading([$data = ''[, $h = '1'[, $attributes = '']]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +020029
Adrian Voicu0cfe1c32015-03-12 22:11:06 +020030 :param string $data: Content
31 :param string $h: Heading level
32 :param mixed $attributes: HTML attributes
33 :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
Adrian Voicu875d5a12015-03-12 16:42:50 +020044 classes, ids or inline styles, a third parameter is available either
45 as a string or as an array::
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Adrian Voicu875d5a12015-03-12 16:42:50 +020047 echo heading('Welcome!', 3, 'class="pink"');
Adrian Voicu0cfe1c32015-03-12 22:11:06 +020048 echo heading('How are you?', 4, array('id' => 'question', 'class' => 'green'));
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Derek Jones0f0304c2013-07-21 11:35:30 -070050 The above code produces:
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jones0f0304c2013-07-21 11:35:30 -070052 .. code-block:: html
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Derek Jones0f0304c2013-07-21 11:35:30 -070054 <h3 class="pink">Welcome!<h3>
Adrian Voicu8e2f83d2015-03-12 17:13:47 +020055 <h4 id="question" class="green">How are you?</h4>
Derek Jones0f0304c2013-07-21 11:35:30 -070056
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020057.. php:function:: img([$src = ''[, $index_page = FALSE[, $attributes = '']]])
Derek Jones8ede1a22011-10-05 13:34:52 -050058
Andrey Andreev53b8ef52012-11-08 21:38:53 +020059 :param string $src: Image source data
60 :param bool $index_page: Whether to treat $src as a routed URI string
61 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +020062 :returns: HTML image tag
63 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +020064
Derek Jones0f0304c2013-07-21 11:35:30 -070065 Lets you create HTML <img /> tags. The first parameter contains the
66 image source. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Derek Jones0f0304c2013-07-21 11:35:30 -070068 echo img('images/picture.jpg'); // gives <img src="http://site.com/images/picture.jpg" />
Derek Jones8ede1a22011-10-05 13:34:52 -050069
Derek Jones0f0304c2013-07-21 11:35:30 -070070 There is an optional second parameter that is a TRUE/FALSE value that
71 specifics if the *src* should have the page specified by
72 ``$config['index_page']`` added to the address it creates.
73 Presumably, this would be if you were using a media controller::
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Derek Jones0f0304c2013-07-21 11:35:30 -070075 echo img('images/picture.jpg', TRUE); // gives <img src="http://site.com/index.php/images/picture.jpg" alt="" />
76
77 Additionally, an associative array can be passed to the ``img()`` function
78 for complete control over all attributes and values. If an *alt* attribute
79 is not provided, CodeIgniter will generate an empty string.
80
81 Example::
82
83 $image_properties = array(
84 'src' => 'images/picture.jpg',
85 'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time',
86 'class' => 'post_images',
87 'width' => '200',
88 'height'=> '200',
89 'title' => 'That was quite a night',
90 'rel' => 'lightbox'
91 );
92
93 img($image_properties);
94 // <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 -050095
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020096.. php:function:: link_tag([$href = ''[, $rel = 'stylesheet'[, $type = 'text/css'[, $title = ''[, $media = ''[, $index_page = FALSE]]]]]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +020097
98 :param string $href: What are we linking to
99 :param string $rel: Relation type
100 :param string $type: Type of the related document
101 :param string $title: Link title
102 :param string $media: Media type
103 :param bool $index_page: Whether to treat $src as a routed URI string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200104 :returns: HTML link tag
105 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Derek Jones0f0304c2013-07-21 11:35:30 -0700107 Lets you create HTML <link /> tags. This is useful for stylesheet links,
108 as well as other links. The parameters are *href*, with optional *rel*,
109 *type*, *title*, *media* and *index_page*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Derek Jones0f0304c2013-07-21 11:35:30 -0700111 *index_page* is a boolean value that specifies if the *href* should have
112 the page specified by ``$config['index_page']`` added to the address it creates.
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Derek Jones0f0304c2013-07-21 11:35:30 -0700114 Example::
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200115
Derek Jones0f0304c2013-07-21 11:35:30 -0700116 echo link_tag('css/mystyles.css');
117 // gives <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
118
119 Further examples::
120
121 echo link_tag('favicon.ico', 'shortcut icon', 'image/ico');
122 // <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />
123
124 echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
125 // <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />
126
127 Additionally, an associative array can be passed to the ``link()`` function
128 for complete control over all attributes and values::
129
130 $link = array(
131 'href' => 'css/printer.css',
132 'rel' => 'stylesheet',
133 'type' => 'text/css',
134 'media' => 'print'
135 );
136
137 echo link_tag($link);
138 // <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Derek Jones0f0304c2013-07-21 11:35:30 -0700140
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200141.. php:function:: ul($list[, $attributes = ''])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200142
143 :param array $list: List entries
144 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200145 :returns: HTML-formatted unordered list
146 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200147
Derek Jones0f0304c2013-07-21 11:35:30 -0700148 Permits you to generate ordered or unordered HTML lists from simple or
149 multi-dimensional arrays. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Derek Jones0f0304c2013-07-21 11:35:30 -0700151 $list = array(
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200152 'red',
153 'blue',
Derek Jones0f0304c2013-07-21 11:35:30 -0700154 'green',
155 'yellow'
156 );
157
158 $attributes = array(
159 'class' => 'boldlist',
160 'id' => 'mylist'
161 );
162
163 echo ul($list, $attributes);
164
165 The above code will produce this:
166
167 .. code-block:: html
168
169 <ul class="boldlist" id="mylist">
170 <li>red</li>
171 <li>blue</li>
172 <li>green</li>
173 <li>yellow</li>
174 </ul>
175
176 Here is a more complex example, using a multi-dimensional array::
177
178 $attributes = array(
179 'class' => 'boldlist',
180 'id' => 'mylist'
181 );
182
183 $list = array(
184 'colors' => array(
185 'red',
186 'blue',
187 'green'
188 ),
189 'shapes' => array(
190 'round',
191 'square',
192 'circles' => array(
193 'ellipse',
194 'oval',
195 'sphere'
196 )
197 ),
198 'moods' => array(
199 'happy',
200 'upset' => array(
201 'defeated' => array(
202 'dejected',
203 'disheartened',
204 'depressed'
205 ),
206 'annoyed',
207 'cross',
208 'angry'
209 )
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200210 )
Derek Jones0f0304c2013-07-21 11:35:30 -0700211 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
Derek Jones0f0304c2013-07-21 11:35:30 -0700213 echo ul($list, $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
Derek Jones0f0304c2013-07-21 11:35:30 -0700215 The above code will produce this:
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Derek Jones0f0304c2013-07-21 11:35:30 -0700217 .. code-block:: html
218
219 <ul class="boldlist" id="mylist">
220 <li>colors
221 <ul>
222 <li>red</li>
223 <li>blue</li>
224 <li>green</li>
225 </ul>
226 </li>
227 <li>shapes
228 <ul>
229 <li>round</li>
230 <li>suare</li>
231 <li>circles
232 <ul>
233 <li>elipse</li>
234 <li>oval</li>
235 <li>sphere</li>
236 </ul>
237 </li>
238 </ul>
239 </li>
240 <li>moods
241 <ul>
242 <li>happy</li>
243 <li>upset
244 <ul>
245 <li>defeated
246 <ul>
247 <li>dejected</li>
248 <li>disheartened</li>
249 <li>depressed</li>
250 </ul>
251 </li>
252 <li>annoyed</li>
253 <li>cross</li>
254 <li>angry</li>
255 </ul>
256 </li>
257 </ul>
258 </li>
259 </ul>
Derek Jones8ede1a22011-10-05 13:34:52 -0500260
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200261.. php:function:: ol($list, $attributes = '')
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200262
263 :param array $list: List entries
264 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200265 :returns: HTML-formatted ordered list
266 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200267
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200268 Identical to :php:func:`ul()`, only it produces the <ol> tag for
Derek Jones0f0304c2013-07-21 11:35:30 -0700269 ordered lists instead of <ul>.
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200270
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200271.. php:function:: meta([$name = ''[, $content = ''[, $type = 'name'[, $newline = "\n"]]]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200273 :param string $name: Meta name
274 :param string $content: Meta content
275 :param string $type: Meta type
276 :param string $newline: Newline character
Andrey Andreev3de130c2014-02-07 23:31:49 +0200277 :returns: HTML meta tag
278 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200279
Derek Jones0f0304c2013-07-21 11:35:30 -0700280 Helps you generate meta tags. You can pass strings to the function, or
281 simple arrays, or multidimensional ones.
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200282
Derek Jones0f0304c2013-07-21 11:35:30 -0700283 Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
Derek Jones0f0304c2013-07-21 11:35:30 -0700285 echo meta('description', 'My Great site');
286 // Generates: <meta name="description" content="My Great Site" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
Derek Jones0f0304c2013-07-21 11:35:30 -0700288 echo meta('Content-type', 'text/html; charset=utf-8', 'equiv');
289 // Note the third parameter. Can be "equiv" or "name"
290 // Generates: <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500291
Derek Jones0f0304c2013-07-21 11:35:30 -0700292 echo meta(array('name' => 'robots', 'content' => 'no-cache'));
293 // Generates: <meta name="robots" content="no-cache" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500294
Derek Jones0f0304c2013-07-21 11:35:30 -0700295 $meta = array(
296 array(
297 'name' => 'robots',
298 'content' => 'no-cache'
299 ),
300 array(
301 'name' => 'description',
302 'content' => 'My Great Site'
303 ),
304 array(
305 'name' => 'keywords',
306 'content' => 'love, passion, intrigue, deception'
307 ),
308 array(
309 'name' => 'robots',
310 'content' => 'no-cache'
311 ),
312 array(
313 'name' => 'Content-type',
314 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'
315 )
316 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500317
Derek Jones0f0304c2013-07-21 11:35:30 -0700318 echo meta($meta);
319 // Generates:
320 // <meta name="robots" content="no-cache" />
321 // <meta name="description" content="My Great Site" />
322 // <meta name="keywords" content="love, passion, intrigue, deception" />
323 // <meta name="robots" content="no-cache" />
324 // <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500325
Derek Jones8ede1a22011-10-05 13:34:52 -0500326
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200327.. php:function:: doctype([$type = 'xhtml1-strict'])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200328
329 :param string $type: Doctype name
Andrey Andreev3de130c2014-02-07 23:31:49 +0200330 :returns: HTML DocType tag
331 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200332
Derek Jones0f0304c2013-07-21 11:35:30 -0700333 Helps you generate document type declarations, or DTD's. XHTML 1.0
334 Strict is used by default, but many doctypes are available.
Derek Jones8ede1a22011-10-05 13:34:52 -0500335
Derek Jones0f0304c2013-07-21 11:35:30 -0700336 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500337
Derek Jones0f0304c2013-07-21 11:35:30 -0700338 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 -0500339
Derek Jones0f0304c2013-07-21 11:35:30 -0700340 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 -0500341
Derek Jones0f0304c2013-07-21 11:35:30 -0700342 The following is a list of doctype choices. These are configurable, and
343 pulled from application/config/doctypes.php
Derek Jones8ede1a22011-10-05 13:34:52 -0500344
Andrey Andreev3de130c2014-02-07 23:31:49 +0200345 =============================== =================== ==================================================================================================================================================
346 Document type Option Result
347 =============================== =================== ==================================================================================================================================================
348 XHTML 1.1 xhtml11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
349 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">
350 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">
351 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">
352 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">
353 HTML 5 html5 <!DOCTYPE html>
354 HTML 4 Strict html4-strict <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
355 HTML 4 Transitional html4-trans <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
356 HTML 4 Frameset html4-frame <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
357 MathML 1.01 mathml1 <!DOCTYPE math SYSTEM "http://www.w3.org/Math/DTD/mathml1/mathml.dtd">
358 MathML 2.0 mathml2 <!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
359 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">
360 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">
361 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">
362 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">
363 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">
364 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">
365 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">
366 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 +0200367 =============================== =================== ==================================================================================================================================================
368
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200369.. php:function:: br([$count = 1])
Andrey Andreev59f04262014-02-26 19:04:36 +0200370
371 :param int $count: Number of times to repeat the tag
372 :returns: HTML line break tag
373 :rtype: string
374
375 Generates line break tags (<br />) based on the number you submit.
376 Example::
377
378 echo br(3);
379
380 The above would produce:
381
382 .. code-block:: html
383
384 <br /><br /><br />
385
386 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
387 in combination with ``<br />`` instead.
388
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200389.. php:function:: nbs([$num = 1])
Andrey Andreev59f04262014-02-26 19:04:36 +0200390
391 :param int $num: Number of space entities to produce
392 :returns: A sequence of non-breaking space HTML entities
393 :rtype: string
394
395 Generates non-breaking spaces (&nbsp;) based on the number you submit.
396 Example::
397
398 echo nbs(3);
399
400 The above would produce:
401
402 .. code-block:: html
403
404 &nbsp;&nbsp;&nbsp;
405
406 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
Adrian Voicu875d5a12015-03-12 16:42:50 +0200407 in combination with ``&nbsp;`` instead.