Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | HTML Helper |
| 3 | ########### |
| 4 | |
| 5 | The HTML Helper file contains functions that assist in working with |
| 6 | HTML. |
| 7 | |
| 8 | .. contents:: Page Contents |
| 9 | |
| 10 | Loading this Helper |
| 11 | =================== |
| 12 | |
| 13 | This helper is loaded using the following code:: |
| 14 | |
| 15 | $this->load->helper('html'); |
| 16 | |
| 17 | The following functions are available: |
| 18 | |
| 19 | br() |
| 20 | ==== |
| 21 | |
| 22 | Generates line break tags (<br />) based on the number you submit. |
| 23 | Example:: |
| 24 | |
| 25 | echo br(3); |
| 26 | |
| 27 | The above would produce: <br /><br /><br /> |
| 28 | |
| 29 | heading() |
| 30 | ========= |
| 31 | |
| 32 | Lets you create HTML <h1> tags. The first parameter will contain the |
| 33 | data, the second the size of the heading. Example:: |
| 34 | |
| 35 | echo heading('Welcome!', 3); |
| 36 | |
| 37 | The above would produce: <h3>Welcome!</h3> |
| 38 | |
| 39 | Additionally, in order to add attributes to the heading tag such as HTML |
| 40 | classes, ids or inline styles, a third parameter is available. |
| 41 | |
| 42 | :: |
| 43 | |
| 44 | echo heading('Welcome!', 3, 'class="pink"') |
| 45 | |
| 46 | The above code produces: <h3 class="pink">Welcome!<<h3> |
| 47 | |
| 48 | img() |
| 49 | ===== |
| 50 | |
| 51 | Lets you create HTML <img /> tags. The first parameter contains the |
| 52 | image source. Example |
| 53 | |
| 54 | :: |
| 55 | |
| 56 | echo img('images/picture.jpg'); // gives <img src="http://site.com/images/picture.jpg" /> |
| 57 | |
| 58 | There is an optional second parameter that is a TRUE/FALSE value that |
| 59 | specifics if the src should have the page specified by |
| 60 | $config['index_page'] added to the address it creates. Presumably, this |
| 61 | would be if you were using a media controller. |
| 62 | |
| 63 | :: |
| 64 | |
| 65 | echo img('images/picture.jpg', TRUE); // gives <img src="http://site.com/index.php/images/picture.jpg" alt="" /> |
| 66 | |
| 67 | |
| 68 | Additionally, an associative array can be passed to the img() function |
| 69 | for complete control over all attributes and values. If an alt attribute |
| 70 | is not provided, CodeIgniter will generate an empty string. |
| 71 | |
| 72 | :: |
| 73 | |
| 74 | $image_properties = array( |
| 75 | 'src' => 'images/picture.jpg', |
| 76 | 'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time', |
| 77 | 'class' => 'post_images', |
| 78 | 'width' => '200', |
| 79 | 'height'=> '200', |
| 80 | 'title' => 'That was quite a night', |
| 81 | 'rel' => 'lightbox' |
| 82 | ); |
| 83 | |
| 84 | img($image_properties); // <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" /> |
| 85 | |
| 86 | |
| 87 | link_tag() |
| 88 | =========== |
| 89 | |
| 90 | Lets you create HTML <link /> tags. This is useful for stylesheet links, |
| 91 | as well as other links. The parameters are href, with optional rel, |
| 92 | type, title, media and index_page. index_page is a TRUE/FALSE value |
| 93 | that specifics if the href should have the page specified by |
| 94 | $config['index_page'] added to the address it creates. |
| 95 | |
| 96 | :: |
| 97 | |
| 98 | echo link_tag('css/mystyles.css'); // gives <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" /> |
| 99 | |
| 100 | |
| 101 | Further examples |
| 102 | |
| 103 | :: |
| 104 | |
| 105 | echo link_tag('favicon.ico', 'shortcut icon', 'image/ico'); // <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" /> |
| 106 | |
| 107 | echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed'); // <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" /> |
| 108 | |
| 109 | Additionally, an associative array can be passed to the link() function |
| 110 | for complete control over all attributes and values. |
| 111 | |
| 112 | :: |
| 113 | |
| 114 | $link = array( |
| 115 | 'href' => 'css/printer.css', |
| 116 | 'rel' => 'stylesheet', |
| 117 | 'type' => 'text/css', |
| 118 | 'media' => 'print' |
| 119 | ); |
| 120 | |
| 121 | echo link_tag($link); // <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" /> |
| 122 | |
| 123 | |
| 124 | nbs() |
| 125 | ===== |
| 126 | |
| 127 | Generates non-breaking spaces ( ) based on the number you submit. |
| 128 | Example |
| 129 | |
| 130 | :: |
| 131 | |
| 132 | echo nbs(3); |
| 133 | |
| 134 | The above would produce |
| 135 | |
| 136 | :: |
| 137 | |
| 138 | |
| 139 | |
| 140 | ol() and ul() |
| 141 | ============= |
| 142 | |
| 143 | Permits you to generate ordered or unordered HTML lists from simple or |
| 144 | multi-dimensional arrays. Example |
| 145 | |
| 146 | :: |
| 147 | |
| 148 | $this->load->helper('html'); |
| 149 | |
| 150 | $list = array( |
| 151 | 'red', |
| 152 | 'blue', |
| 153 | 'green', |
| 154 | 'yellow' |
| 155 | ); |
| 156 | |
| 157 | $attributes = array( |
| 158 | 'class' => 'boldlist', |
| 159 | 'id' => 'mylist' |
| 160 | ); |
| 161 | |
| 162 | echo ul($list, $attributes); |
| 163 | |
| 164 | The above code will produce this |
| 165 | |
| 166 | :: |
| 167 | |
| 168 | <ul class="boldlist" id="mylist"> |
| 169 | <li>red</li> |
| 170 | <li>blue</li> |
| 171 | <li>green</li> |
| 172 | <li>yellow</li> |
| 173 | </ul> |
| 174 | |
| 175 | Here is a more complex example, using a multi-dimensional array |
| 176 | |
| 177 | :: |
| 178 | |
| 179 | $this->load->helper('html'); |
| 180 | |
| 181 | $attributes = array( |
| 182 | 'class' => 'boldlist', |
| 183 | 'id' => 'mylist' |
| 184 | ); |
| 185 | |
| 186 | $list = array( |
| 187 | 'colors' => array( |
| 188 | 'red', |
| 189 | 'blue', |
| 190 | 'green' |
| 191 | ), |
| 192 | 'shapes' => array( |
| 193 | 'round', |
| 194 | 'square', |
| 195 | 'circles' => array( |
| 196 | 'ellipse', |
| 197 | 'oval', |
| 198 | 'sphere' |
| 199 | ) |
| 200 | ), |
| 201 | 'moods' => array( |
| 202 | 'happy', |
| 203 | 'upset' => array( |
| 204 | 'defeated' => array( |
| 205 | 'dejected', |
| 206 | 'disheartened', |
| 207 | 'depressed' |
| 208 | ), |
| 209 | 'annoyed', |
| 210 | 'cross', |
| 211 | 'angry' |
| 212 | ) |
| 213 | ) |
| 214 | ); |
| 215 | |
| 216 | echo ul($list, $attributes); |
| 217 | |
| 218 | The above code will produce this |
| 219 | |
| 220 | :: |
| 221 | |
| 222 | <ul class="boldlist" id="mylist"> |
| 223 | <li>colors |
| 224 | <ul> |
| 225 | <li>red</li> |
| 226 | <li>blue</li> |
| 227 | <li>green</li> |
| 228 | </ul> |
| 229 | </li> |
| 230 | <li>shapes |
| 231 | <ul> |
| 232 | <li>round</li> |
| 233 | <li>suare</li> |
| 234 | <li>circles |
| 235 | <ul> |
| 236 | <li>elipse</li> |
| 237 | <li>oval</li> |
| 238 | <li>sphere</li> |
| 239 | </ul> |
| 240 | </li> |
| 241 | </ul> |
| 242 | </li> |
| 243 | <li>moods |
| 244 | <ul> |
| 245 | <li>happy</li> |
| 246 | <li>upset |
| 247 | <ul> |
| 248 | <li>defeated |
| 249 | <ul> |
| 250 | <li>dejected</li> |
| 251 | <li>disheartened</li> |
| 252 | <li>depressed</li> |
| 253 | </ul> |
| 254 | </li> |
| 255 | <li>annoyed</li> |
| 256 | <li>cross</li> |
| 257 | <li>angry</li> |
| 258 | </ul> |
| 259 | </li> |
| 260 | </ul> |
| 261 | </li> |
| 262 | </ul> |
| 263 | |
| 264 | meta() |
| 265 | ====== |
| 266 | |
| 267 | Helps you generate meta tags. You can pass strings to the function, or |
| 268 | simple arrays, or multidimensional ones. Examples |
| 269 | |
| 270 | :: |
| 271 | |
| 272 | echo meta('description', 'My Great site'); |
| 273 | // Generates: <meta name="description" content="My Great Site" /> |
| 274 | |
| 275 | echo meta('Content-type', 'text/html; charset=utf-8', 'equiv'); |
| 276 | // Note the third parameter. Can be "equiv" or "name" |
| 277 | // Generates: <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> |
| 278 | |
| 279 | echo meta(array('name' => 'robots', 'content' => 'no-cache')); |
| 280 | // Generates: <meta name="robots" content="no-cache" /> |
| 281 | |
| 282 | $meta = array( |
| 283 | array( |
| 284 | 'name' => 'robots', |
| 285 | 'content' => 'no-cache' |
| 286 | ), |
| 287 | array( |
| 288 | 'name' => 'description', |
| 289 | 'content' => 'My Great Site' |
| 290 | ), |
| 291 | array( |
| 292 | 'name' => 'keywords', |
| 293 | 'content' => 'love, passion, intrigue, deception' |
| 294 | ), |
| 295 | array( |
| 296 | 'name' => 'robots', |
| 297 | 'content' => 'no-cache' |
| 298 | ), |
| 299 | array( |
| 300 | 'name' => 'Content-type', |
| 301 | 'content' => 'text/html; charset=utf-8', 'type' => 'equiv' |
| 302 | ) |
| 303 | ); |
| 304 | |
| 305 | echo meta($meta); |
| 306 | // Generates: |
| 307 | // <meta name="robots" content="no-cache" /> |
| 308 | // <meta name="description" content="My Great Site" /> |
| 309 | // <meta name="keywords" content="love, passion, intrigue, deception" /> |
| 310 | // <meta name="robots" content="no-cache" /> |
| 311 | // <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> |
| 312 | |
| 313 | doctype() |
| 314 | ========= |
| 315 | |
| 316 | Helps you generate document type declarations, or DTD's. XHTML 1.0 |
| 317 | Strict is used by default, but many doctypes are available. |
| 318 | |
| 319 | :: |
| 320 | |
| 321 | echo doctype(); // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 322 | |
| 323 | echo doctype('html4-trans'); // <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 324 | |
| 325 | The following is a list of doctype choices. These are configurable, and |
| 326 | pulled from application/config/doctypes.php |
| 327 | |
| 328 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 329 | | Doctype | Option | Result | |
| 330 | +========================+==========================+===========================================================================================================================+ |
| 331 | | XHTML 1.1 | doctype('xhtml11') | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
| 332 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 333 | | XHTML 1.0 Strict | doctype('xhtml1-strict') | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
| 334 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 335 | | XHTML 1.0 Transitional | doctype('xhtml1-trans') | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
| 336 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 337 | | XHTML 1.0 Frameset | doctype('xhtml1-frame') | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> | |
| 338 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 339 | | XHTML Basic 1.1 | doctype('xhtml-basic11') | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> | |
| 340 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 341 | | HTML 5 | doctype('html5') | <!DOCTYPE html> | |
| 342 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 343 | | HTML 4 Strict | doctype('html4-strict') | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
| 344 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 345 | | HTML 4 Transitional | doctype('html4-trans') | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
| 346 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |
| 347 | | HTML 4 Frameset | doctype('html4-frame') | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> | |
| 348 | +------------------------+--------------------------+---------------------------------------------------------------------------------------------------------------------------+ |