blob: 3324da8c0f4a7fd79a540800e1f01c9042455d28 [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:: br([$count = 1])
Andrey Andreev53b8ef52012-11-08 21:38:53 +020029
30 :param int $count: Number of times to repeat the tag
Andrey Andreev3de130c2014-02-07 23:31:49 +020031 :returns: HTML line break tag
32 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +020033
Derek Jones0f0304c2013-07-21 11:35:30 -070034 Generates line break tags (<br />) based on the number you submit.
35 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Derek Jones0f0304c2013-07-21 11:35:30 -070037 echo br(3);
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Derek Jones0f0304c2013-07-21 11:35:30 -070039 The above would produce:
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jones0f0304c2013-07-21 11:35:30 -070041 .. code-block:: html
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Derek Jones0f0304c2013-07-21 11:35:30 -070043 <br /><br /><br />
44
Derek Jones0f0304c2013-07-21 11:35:30 -070045.. function:: heading([$data = ''[, $h = '1'[, $attributes = '']]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +020046
47 :param string $data: Content
48 :param string $h: Heading level
49 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +020050 :returns: HTML heading tag
51 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +020052
Derek Jones0f0304c2013-07-21 11:35:30 -070053 Lets you create HTML heading tags. The first parameter will contain the
54 data, the second the size of the heading. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Derek Jones0f0304c2013-07-21 11:35:30 -070056 echo heading('Welcome!', 3);
Derek Jones8ede1a22011-10-05 13:34:52 -050057
Derek Jones0f0304c2013-07-21 11:35:30 -070058 The above would produce: <h3>Welcome!</h3>
Derek Jones8ede1a22011-10-05 13:34:52 -050059
Derek Jones0f0304c2013-07-21 11:35:30 -070060 Additionally, in order to add attributes to the heading tag such as HTML
61 classes, ids or inline styles, a third parameter is available::
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Derek Jones0f0304c2013-07-21 11:35:30 -070063 echo heading('Welcome!', 3, 'class="pink"')
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Derek Jones0f0304c2013-07-21 11:35:30 -070065 The above code produces:
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Derek Jones0f0304c2013-07-21 11:35:30 -070067 .. code-block:: html
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Derek Jones0f0304c2013-07-21 11:35:30 -070069 <h3 class="pink">Welcome!<h3>
70
Derek Jones0f0304c2013-07-21 11:35:30 -070071.. function:: img([$src = ''[, $index_page = FALSE[, $attributes = '']]])
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev53b8ef52012-11-08 21:38:53 +020073 :param string $src: Image source data
74 :param bool $index_page: Whether to treat $src as a routed URI string
75 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +020076 :returns: HTML image tag
77 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +020078
Derek Jones0f0304c2013-07-21 11:35:30 -070079 Lets you create HTML <img /> tags. The first parameter contains the
80 image source. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050081
Derek Jones0f0304c2013-07-21 11:35:30 -070082 echo img('images/picture.jpg'); // gives <img src="http://site.com/images/picture.jpg" />
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Derek Jones0f0304c2013-07-21 11:35:30 -070084 There is an optional second parameter that is a TRUE/FALSE value that
85 specifics if the *src* should have the page specified by
86 ``$config['index_page']`` added to the address it creates.
87 Presumably, this would be if you were using a media controller::
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jones0f0304c2013-07-21 11:35:30 -070089 echo img('images/picture.jpg', TRUE); // gives <img src="http://site.com/index.php/images/picture.jpg" alt="" />
90
91 Additionally, an associative array can be passed to the ``img()`` function
92 for complete control over all attributes and values. If an *alt* attribute
93 is not provided, CodeIgniter will generate an empty string.
94
95 Example::
96
97 $image_properties = array(
98 'src' => 'images/picture.jpg',
99 'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time',
100 'class' => 'post_images',
101 'width' => '200',
102 'height'=> '200',
103 'title' => 'That was quite a night',
104 'rel' => 'lightbox'
105 );
106
107 img($image_properties);
108 // <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 -0500109
Andrey Andreev3de130c2014-02-07 23:31:49 +0200110.. function:: link_tag([$href = ''[, $rel = 'stylesheet'[, $type = 'text/css'[, $title = ''[, $media = ''[, $index_page = FALSE]]]]]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200111
112 :param string $href: What are we linking to
113 :param string $rel: Relation type
114 :param string $type: Type of the related document
115 :param string $title: Link title
116 :param string $media: Media type
117 :param bool $index_page: Whether to treat $src as a routed URI string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200118 :returns: HTML link tag
119 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Derek Jones0f0304c2013-07-21 11:35:30 -0700121 Lets you create HTML <link /> tags. This is useful for stylesheet links,
122 as well as other links. The parameters are *href*, with optional *rel*,
123 *type*, *title*, *media* and *index_page*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Derek Jones0f0304c2013-07-21 11:35:30 -0700125 *index_page* is a boolean value that specifies if the *href* should have
126 the page specified by ``$config['index_page']`` added to the address it creates.
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Derek Jones0f0304c2013-07-21 11:35:30 -0700128 Example::
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200129
Derek Jones0f0304c2013-07-21 11:35:30 -0700130 echo link_tag('css/mystyles.css');
131 // gives <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
132
133 Further examples::
134
135 echo link_tag('favicon.ico', 'shortcut icon', 'image/ico');
136 // <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />
137
138 echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
139 // <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />
140
141 Additionally, an associative array can be passed to the ``link()`` function
142 for complete control over all attributes and values::
143
144 $link = array(
145 'href' => 'css/printer.css',
146 'rel' => 'stylesheet',
147 'type' => 'text/css',
148 'media' => 'print'
149 );
150
151 echo link_tag($link);
152 // <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Derek Jones0f0304c2013-07-21 11:35:30 -0700154.. function:: nbs([$num = 1])
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200156 :param int $num: Number of space entities to produce
Andrey Andreev3de130c2014-02-07 23:31:49 +0200157 :returns: A sequence of non-breaking space HTML entities
158 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200159
Derek Jones0f0304c2013-07-21 11:35:30 -0700160 Generates non-breaking spaces (&nbsp;) based on the number you submit.
161 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Derek Jones0f0304c2013-07-21 11:35:30 -0700163 echo nbs(3);
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Derek Jones0f0304c2013-07-21 11:35:30 -0700165 The above would produce:
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Derek Jones0f0304c2013-07-21 11:35:30 -0700167 .. code-block:: html
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones0f0304c2013-07-21 11:35:30 -0700169 &nbsp;&nbsp;&nbsp;
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Derek Jones0f0304c2013-07-21 11:35:30 -0700171
172.. function:: ul($list[, $attributes = ''])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200173
174 :param array $list: List entries
175 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200176 :returns: HTML-formatted unordered list
177 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200178
Derek Jones0f0304c2013-07-21 11:35:30 -0700179 Permits you to generate ordered or unordered HTML lists from simple or
180 multi-dimensional arrays. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Derek Jones0f0304c2013-07-21 11:35:30 -0700182 $list = array(
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200183 'red',
184 'blue',
Derek Jones0f0304c2013-07-21 11:35:30 -0700185 'green',
186 'yellow'
187 );
188
189 $attributes = array(
190 'class' => 'boldlist',
191 'id' => 'mylist'
192 );
193
194 echo ul($list, $attributes);
195
196 The above code will produce this:
197
198 .. code-block:: html
199
200 <ul class="boldlist" id="mylist">
201 <li>red</li>
202 <li>blue</li>
203 <li>green</li>
204 <li>yellow</li>
205 </ul>
206
207 Here is a more complex example, using a multi-dimensional array::
208
209 $attributes = array(
210 'class' => 'boldlist',
211 'id' => 'mylist'
212 );
213
214 $list = array(
215 'colors' => array(
216 'red',
217 'blue',
218 'green'
219 ),
220 'shapes' => array(
221 'round',
222 'square',
223 'circles' => array(
224 'ellipse',
225 'oval',
226 'sphere'
227 )
228 ),
229 'moods' => array(
230 'happy',
231 'upset' => array(
232 'defeated' => array(
233 'dejected',
234 'disheartened',
235 'depressed'
236 ),
237 'annoyed',
238 'cross',
239 'angry'
240 )
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200241 )
Derek Jones0f0304c2013-07-21 11:35:30 -0700242 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500243
Derek Jones0f0304c2013-07-21 11:35:30 -0700244 echo ul($list, $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
Derek Jones0f0304c2013-07-21 11:35:30 -0700246 The above code will produce this:
Derek Jones8ede1a22011-10-05 13:34:52 -0500247
Derek Jones0f0304c2013-07-21 11:35:30 -0700248 .. code-block:: html
249
250 <ul class="boldlist" id="mylist">
251 <li>colors
252 <ul>
253 <li>red</li>
254 <li>blue</li>
255 <li>green</li>
256 </ul>
257 </li>
258 <li>shapes
259 <ul>
260 <li>round</li>
261 <li>suare</li>
262 <li>circles
263 <ul>
264 <li>elipse</li>
265 <li>oval</li>
266 <li>sphere</li>
267 </ul>
268 </li>
269 </ul>
270 </li>
271 <li>moods
272 <ul>
273 <li>happy</li>
274 <li>upset
275 <ul>
276 <li>defeated
277 <ul>
278 <li>dejected</li>
279 <li>disheartened</li>
280 <li>depressed</li>
281 </ul>
282 </li>
283 <li>annoyed</li>
284 <li>cross</li>
285 <li>angry</li>
286 </ul>
287 </li>
288 </ul>
289 </li>
290 </ul>
Derek Jones8ede1a22011-10-05 13:34:52 -0500291
Derek Jonesb8c283a2013-07-19 16:02:53 -0700292.. function:: ol($list, $attributes = '')
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200293
294 :param array $list: List entries
295 :param array $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200296 :returns: HTML-formatted ordered list
297 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200298
Derek Jones0f0304c2013-07-21 11:35:30 -0700299 Identical to :func:`ul()`, only it produces the <ol> tag for
300 ordered lists instead of <ul>.
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200301
Derek Jones0f0304c2013-07-21 11:35:30 -0700302.. function:: meta([$name = ''[, $content = ''[, $type = 'name'[, $newline = "\n"]]]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200304 :param string $name: Meta name
305 :param string $content: Meta content
306 :param string $type: Meta type
307 :param string $newline: Newline character
Andrey Andreev3de130c2014-02-07 23:31:49 +0200308 :returns: HTML meta tag
309 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200310
Derek Jones0f0304c2013-07-21 11:35:30 -0700311 Helps you generate meta tags. You can pass strings to the function, or
312 simple arrays, or multidimensional ones.
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200313
Derek Jones0f0304c2013-07-21 11:35:30 -0700314 Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500315
Derek Jones0f0304c2013-07-21 11:35:30 -0700316 echo meta('description', 'My Great site');
317 // Generates: <meta name="description" content="My Great Site" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500318
Derek Jones0f0304c2013-07-21 11:35:30 -0700319 echo meta('Content-type', 'text/html; charset=utf-8', 'equiv');
320 // Note the third parameter. Can be "equiv" or "name"
321 // Generates: <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500322
Derek Jones0f0304c2013-07-21 11:35:30 -0700323 echo meta(array('name' => 'robots', 'content' => 'no-cache'));
324 // Generates: <meta name="robots" content="no-cache" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500325
Derek Jones0f0304c2013-07-21 11:35:30 -0700326 $meta = array(
327 array(
328 'name' => 'robots',
329 'content' => 'no-cache'
330 ),
331 array(
332 'name' => 'description',
333 'content' => 'My Great Site'
334 ),
335 array(
336 'name' => 'keywords',
337 'content' => 'love, passion, intrigue, deception'
338 ),
339 array(
340 'name' => 'robots',
341 'content' => 'no-cache'
342 ),
343 array(
344 'name' => 'Content-type',
345 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'
346 )
347 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500348
Derek Jones0f0304c2013-07-21 11:35:30 -0700349 echo meta($meta);
350 // Generates:
351 // <meta name="robots" content="no-cache" />
352 // <meta name="description" content="My Great Site" />
353 // <meta name="keywords" content="love, passion, intrigue, deception" />
354 // <meta name="robots" content="no-cache" />
355 // <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Derek Jones8ede1a22011-10-05 13:34:52 -0500356
Derek Jones8ede1a22011-10-05 13:34:52 -0500357
Derek Jones0f0304c2013-07-21 11:35:30 -0700358.. function:: doctype([$type = 'xhtml1-strict'])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200359
360 :param string $type: Doctype name
Andrey Andreev3de130c2014-02-07 23:31:49 +0200361 :returns: HTML DocType tag
362 :rtype: string
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200363
Derek Jones0f0304c2013-07-21 11:35:30 -0700364 Helps you generate document type declarations, or DTD's. XHTML 1.0
365 Strict is used by default, but many doctypes are available.
Derek Jones8ede1a22011-10-05 13:34:52 -0500366
Derek Jones0f0304c2013-07-21 11:35:30 -0700367 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500368
Derek Jones0f0304c2013-07-21 11:35:30 -0700369 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 -0500370
Derek Jones0f0304c2013-07-21 11:35:30 -0700371 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 -0500372
Derek Jones0f0304c2013-07-21 11:35:30 -0700373 The following is a list of doctype choices. These are configurable, and
374 pulled from application/config/doctypes.php
Derek Jones8ede1a22011-10-05 13:34:52 -0500375
Andrey Andreev3de130c2014-02-07 23:31:49 +0200376 =============================== =================== ==================================================================================================================================================
377 Document type Option Result
378 =============================== =================== ==================================================================================================================================================
379 XHTML 1.1 xhtml11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
380 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">
381 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">
382 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">
383 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">
384 HTML 5 html5 <!DOCTYPE html>
385 HTML 4 Strict html4-strict <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
386 HTML 4 Transitional html4-trans <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
387 HTML 4 Frameset html4-frame <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
388 MathML 1.01 mathml1 <!DOCTYPE math SYSTEM "http://www.w3.org/Math/DTD/mathml1/mathml.dtd">
389 MathML 2.0 mathml2 <!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
390 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">
391 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">
392 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">
393 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">
394 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">
395 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">
396 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">
397 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">
398 =============================== =================== ==================================================================================================================================================