HTML Helper

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

Loading this Helper

This helper is loaded using the following code:

$this->load->helper('html');

Available Functions

The following functions are available:

heading([$data = ''[, $h = '1'[, $attributes = '']]])
Parameters:
  • $data (string) – Content
  • $h (string) – Heading level
  • $attributes (mixed) – HTML attributes
Returns:

HTML heading tag

Return type:

string

Lets you create HTML heading tags. The first parameter will contain the data, the second the size of the heading. Example:

echo heading('Welcome!', 3);

The above would produce: <h3>Welcome!</h3>

Additionally, in order to add attributes to the heading tag such as HTML classes, ids or inline styles, a third parameter accepts either a string or an array:

echo heading('Welcome!', 3, 'class="pink"');
echo heading('How are you?', 4, array('id' => 'question', 'class' => 'green'));

The above code produces:

<h3 class="pink">Welcome!<h3>
<h4 id="question" class="green">How are you?</h4>
img([$src = ''[, $index_page = FALSE[, $attributes = '']]])
Parameters:
  • $src (string) – Image source data
  • $index_page (bool) – Whether to treat $src as a routed URI string
  • $attributes (array) – HTML attributes
Returns:

HTML image tag

Return type:

string

Lets you create HTML <img /> tags. The first parameter contains the image source. Example:

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

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

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

Additionally, an associative array can be passed to the img() function for complete control over all attributes and values. If an alt attribute is not provided, CodeIgniter will generate an empty string.

Example:

$image_properties = array(
        'src'   => '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'
);

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" />