HTML Helper

The HTML Helper file contains functions that assist in working withHTML.

Loading this Helper

This helper is loaded using the following code:

  1. helper('html');

Available Functions

The following functions are available:

  • img([$src = ''[, $indexPage = false[, $attributes = '']]])

Parameters:

  • $src (mixed) – Image source data
  • $indexPage (bool) – Whether to treat $src as a routed URI string
  • $attributes (mixed) – HTML attributesReturns:HTML image tagReturn type:string

Lets you create HTML HTML Helper - 图1 tags. The first parameter contains theimage source. Example:

  1. echo img('images/picture.jpg');
  2. // <img src="http://site.com/images/picture.jpg" />

There is an optional second parameter that is a true/false value thatspecifics if the src should have the page specified by$config['indexPage'] added to the address it creates.Presumably, this would be if you were using a media controller:

  1. echo img('images/picture.jpg', true);
  2. // <img src="http://site.com/index.php/images/picture.jpg" alt="" />

Additionally, an associative array can be passed as the first parameter,for complete control over all attributes and values. If an alt attributeis not provided, CodeIgniter will generate an empty string.

Example:

  1. $imageProperties = [
  2. 'src' => 'images/picture.jpg',
  3. 'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time',
  4. 'class' => 'post_images',
  5. 'width' => '200',
  6. 'height' => '200',
  7. 'title' => 'That was quite a night',
  8. 'rel' => 'lightbox'
  9. ];
  10.  
  11. img($imageProperties);
  12. // <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" />
  • linktag([$href = ''[, $rel = 'stylesheet'[, $type = 'text/css'[, $title = ''[, $media = ''[, $indexPage = false_]]]]]])

Parameters:

  • $href (string) – The source of the link file
  • $rel (string) – Relation type
  • $type (string) – Type of the related document
  • $title (string) – Link title
  • $media (string) – Media type
  • $indexPage (bool) – Whether to treat $src as a routed URI stringReturns:HTML link tagReturn type:string

Lets you create HTML tags. This is useful for stylesheet links,as well as other links. The parameters are href, with optional rel,type, title, media and indexPage.

indexPage is a boolean value that specifies if the href should havethe page specified by $config['indexPage'] added to the address it creates.

Example:

  1. echo link_tag('css/mystyles.css');
  2. // <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />

Further examples:

  1. echo link_tag('favicon.ico', 'shortcut icon', 'image/ico');
  2. // <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />
  3.  
  4. echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
  5. // <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />

Alternately, an associative array can be passed to the link_tag() functionfor complete control over all attributes and values:

  1. $link = [
  2. 'href' => 'css/printer.css',
  3. 'rel' => 'stylesheet',
  4. 'type' => 'text/css',
  5. 'media' => 'print'
  6. ];
  7.  
  8. echo link_tag($link);
  9. // <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />
  • scripttag([$src = ''[, $indexPage = false_]])

Parameters:

  • $src (mixed) – The source name of a JavaScript file
  • $indexPage (bool) – Whether to treat $src as a routed URI stringReturns:HTML script tagReturn type:string

Lets you create HTML tags. The parameters is src, with optional indexPage.

indexPage is a boolean value that specifies if the src should havethe page specified by $config['indexPage'] added to the address it creates.

Example:

  1. echo script_tag('js/mystyles.js');
  2. // <script src="http://site.com/js/mystyles.js" type="text/javascript"></script>

Alternately, an associative array can be passed to the script_tag() functionfor complete control over all attributes and values:

  1. $script = ['src' => 'js/printer.js'];
  2.  
  3. echo script_tag($script);
  4. // <script src="http://site.com/js/printer.js" type="text/javascript"></script>
  • ul($list[, $attributes = ''])

Parameters:

  • $list (array) – List entries
  • $attributes (array) – HTML attributesReturns:HTML-formatted unordered listReturn type:string

Permits you to generate unordered HTML lists from simple ormulti-dimensional arrays. Example:

  1. $list = [
  2. 'red',
  3. 'blue',
  4. 'green',
  5. 'yellow'
  6. ];
  7.  
  8. $attributes = [
  9. 'class' => 'boldlist',
  10. 'id' => 'mylist'
  11. ];
  12.  
  13. echo ul($list, $attributes);

The above code will produce this:

  1. <ul class="boldlist" id="mylist">
  2. <li>red</li>
  3. <li>blue</li>
  4. <li>green</li>
  5. <li>yellow</li>
  6. </ul>

Here is a more complex example, using a multi-dimensional array:

  1. $attributes = [
  2. 'class' => 'boldlist',
  3. 'id' => 'mylist'
  4. ];
  5.  
  6. $list = [
  7. 'colors' => [
  8. 'red',
  9. 'blue',
  10. 'green'
  11. ],
  12. 'shapes' => [
  13. 'round',
  14. 'square',
  15. 'circles' => [
  16. 'ellipse',
  17. 'oval',
  18. 'sphere'
  19. ]
  20. ],
  21. 'moods' => [
  22. 'happy',
  23. 'upset' => [
  24. 'defeated' => [
  25. 'dejected',
  26. 'disheartened',
  27. 'depressed'
  28. ],
  29. 'annoyed',
  30. 'cross',
  31. 'angry'
  32. ]
  33. ]
  34. ];
  35.  
  36. echo ul($list, $attributes);

The above code will produce this:

  1. <ul class="boldlist" id="mylist">
  2. <li>colors
  3. <ul>
  4. <li>red</li>
  5. <li>blue</li>
  6. <li>green</li>
  7. </ul>
  8. </li>
  9. <li>shapes
  10. <ul>
  11. <li>round</li>
  12. <li>suare</li>
  13. <li>circles
  14. <ul>
  15. <li>elipse</li>
  16. <li>oval</li>
  17. <li>sphere</li>
  18. </ul>
  19. </li>
  20. </ul>
  21. </li>
  22. <li>moods
  23. <ul>
  24. <li>happy</li>
  25. <li>upset
  26. <ul>
  27. <li>defeated
  28. <ul>
  29. <li>dejected</li>
  30. <li>disheartened</li>
  31. <li>depressed</li>
  32. </ul>
  33. </li>
  34. <li>annoyed</li>
  35. <li>cross</li>
  36. <li>angry</li>
  37. </ul>
  38. </li>
  39. </ul>
  40. </li>
  41. </ul>
  • ol($list, $attributes = '')

Parameters:

  • $list (array) – List entries
  • $attributes (array) – HTML attributesReturns:HTML-formatted ordered listReturn type:string

Identical to ul(), only it produces the

    tag forordered lists instead of
      .

  • video($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])

Parameters:

  • $src (mixed) – Either a source string or an array of sources. See source() function
  • $unsupportedMessage (string) – The message to display if the media tag is not supported by the browser
  • $attributes (string) – HTML attributes
  • $tracks (array) – Use the track function inside an array. See track() function
  • $indexPage (bool) – Returns:HTML-formatted video elementReturn type:string

Permits you to generate HTML video element from simple orsource arrays. Example:

  1. $tracks =
  2. [
  3. track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'),
  4. track('subtitles_yes.vtt', 'subtitles', 'yes', 'Norwegian Yes')
  5. ];
  6.  
  7. echo video('test.mp4', 'Your browser does not support the video tag.', 'controls');
  8.  
  9. echo video
  10. (
  11. 'http://www.codeigniter.com/test.mp4',
  12. 'Your browser does not support the video tag.',
  13. 'controls',
  14. $tracks
  15. );
  16.  
  17. echo video
  18. (
  19. [
  20. source('movie.mp4', 'video/mp4', 'class="test"'),
  21. source('movie.ogg', 'video/ogg'),
  22. source('movie.mov', 'video/quicktime'),
  23. source('movie.ogv', 'video/ogv; codecs=dirac, speex')
  24. ],
  25. 'Your browser does not support the video tag.',
  26. 'class="test" controls',
  27. $tracks
  28. );

The above code will produce this:

  1. <video src="test.mp4" controls>
  2. Your browser does not support the video tag.
  3. </video>
  4.  
  5. <video src="http://www.codeigniter.com/test.mp4" controls>
  6. <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
  7. <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
  8. Your browser does not support the video tag.
  9. </video>
  10.  
  11. <video class="test" controls>
  12. <source src="movie.mp4" type="video/mp4" class="test" />
  13. <source src="movie.ogg" type="video/ogg" />
  14. <source src="movie.mov" type="video/quicktime" />
  15. <source src="movie.ogv" type="video/ogv; codecs=dirac, speex" />
  16. <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
  17. <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
  18. Your browser does not support the video tag.
  19. </video>
  • audio($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])

Parameters:

  • $src (mixed) – Either a source string or an array of sources. See source() function
  • $unsupportedMessage (string) – The message to display if the media tag is not supported by the browser
  • $attributes (string) –
  • $tracks (array) – Use the track function inside an array. See track() function
  • $indexPage (bool) – Returns:HTML-formatted audio elementReturn type:string

Identical to video(), only it produces the

.

  • source($src = ''[, $type = false[, $attributes = '']])

Parameters:

  • $src (string) – The path of the media resource
  • $type (bool) – The MIME-type of the resource with optional codecs parameters
  • $attributes (array) – HTML attributesReturns:HTML source tagReturn type:string

Lets you create HTML tags. The first parameter contains thesource source. Example:

  1. echo source('movie.mp4', 'video/mp4', 'class="test"');
  2. // <source src="movie.mp4" type="video/mp4" class="test" />
  • embed($src = ''[, $type = false[, $attributes = ''[, $indexPage = false]]])

Parameters:

  • $src (string) – The path of the resource to embed
  • $type (bool) – MIME-type
  • $attributes (array) – HTML attributes
  • $indexPage (bool) – Returns:HTML embed tagReturn type:string

Lets you create HTML tags. The first parameter contains theembed source. Example:

  1. echo embed('movie.mov', 'video/quicktime', 'class="test"');
  2. // <embed src="movie.mov" type="video/quicktime" class="test"/>
  • object($data = ''[, $type = false[, $attributes = '']])

Parameters:

  • $data (string) – A resource URL
  • $type (bool) – Content-type of the resource
  • $attributes (array) – HTML attributes
  • $params (array) – Use the param function inside an array. See param() functionReturns:HTML object tagReturn type:string

Lets you create HTML tags. The first parameter contains theobject data. Example:

  1. echo object('movie.swf', 'application/x-shockwave-flash', 'class="test"');
  2.  
  3. echo object
  4. (
  5. 'movie.swf',
  6. 'application/x-shockwave-flash',
  7. 'class="test"',
  8. [
  9. param('foo', 'bar', 'ref', 'class="test"'),
  10. param('hello', 'world', 'ref', 'class="test"')
  11. ]
  12. );

The above code will produce this:

<object data="movie.swf" class="test"></object>

<object data="movie.swf" class="test">
  <param name="foo" type="ref" value="bar" class="test" />
  <param name="hello" type="ref" value="world" class="test" />
</object>

  • param($name = ''[, $type = false[, $attributes = '']])

Parameters:

  • $name (string) – The name of the parameter
  • $value (string) – The value of the parameter
  • $attributes (array) – HTML attributesReturns:HTML param tagReturn type:string

Lets you create HTML tags. The first parameter contains theparam source. Example:

echo param('movie.mov', 'video/quicktime', 'class="test"');
// <param src="movie.mov" type="video/quicktime" class="test"/>
  • track($name = ''[, $type = false[, $attributes = '']])

Parameters:

  • $name (string) – The name of the parameter
  • $value (string) – The value of the parameter
  • $attributes (array) – HTML attributesReturns:HTML track tagReturn type:string

Generates a track element to specify timed tracks. The tracks areformatted in WebVTT format. Example:

echo track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No');
// <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
  • doctype([$type = 'html5'])

Parameters:

  • $type (string) – Doctype nameReturns:HTML DocType tagReturn type:string

Helps you generate document type declarations, or DTD’s. HTML 5is used by default, but many doctypes are available.

Example:

echo doctype();
// <!DOCTYPE html>

echo doctype('html4-trans');
// <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

The following is a list of the pre-defined doctype choices. These are configurable,pulled from application/Config/DocTypes.php, or they could be over-ridden in your .env configuration.

Document typeOptionResultXHTML 1.1xhtml11XHTML 1.0 Strictxhtml1-strictXHTML 1.0 Transitionalxhtml1-transXHTML 1.0 Framesetxhtml1-frameXHTML Basic 1.1xhtml-basic11HTML 5html5HTML 4 Stricthtml4-strictHTML 4 Transitionalhtml4-transHTML 4 Framesethtml4-frameMathML 1.01mathml1MathML 2.0mathml2SVG 1.0svg10SVG 1.1 Fullsvg11SVG 1.1 Basicsvg11-basicSVG 1.1 Tinysvg11-tinyXHTML+MathML+SVG (XHTML host)xhtml-math-svg-xhXHTML+MathML+SVG (SVG host)xhtml-math-svg-shXHTML+RDFa 1.0xhtml-rdfa-1XHTML+RDFa 1.1xhtml-rdfa-2