CodeIgniter User Guide Version 1.6.0


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');

The following functions are available:

br()

Generates line break tags (<br />) based on the number you submit. Example:

echo br(3);

The above would produce: <br /><br /><br />

heading()

Lets you create HTML <h1> 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>

link()

Lets you create HTML <link /> tags. The parameters are href, with optional rel, type, title, media and index_page. index_page is a TRUE/FALSE value that specifics if the href should have the page specified by $config['index_page'] added to the address it creates.

While stylesheets are more conveniently handled through the stylesheet() function, they can also be called here.

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

Further examples:

link('favicon.ico', 'shortcut icon', 'image/ico');
// <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />

link('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
// <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />

Additionally, an associative array can be passed to the link() function for complete control over all attributes and values.

$link = array(
          'href' => 'css/printer.css',
          'rel' => 'stylesheet',
          'type' => 'text/css',
          'media' => 'print'
);

link($link);
// <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />

stylesheet()

The stylesheet() function simplifies the linking of CSS into your document. The arguments are stylesheet(href, optional rel, optional title, optional media).

For more complete control over the output of the tag, you may want to use the link() function.

stylesheet('foo.css');
// <link href="http://site.com/foo.css" rel="stylesheet" type="text/css" media="screen" />

stylesheet('foo.css', 'alternate stylesheet', 'black and white version', 'screen') .'<br>';
// <link href="http://site.com/foo.css" rel="alternate stylesheet" type="text/css" media="screen" title="black and white version" />

nbs()

Generates non-breaking spaces (&nbsp;) based on the number you submit. Example:

echo nbs(3);

The above would produce: &nbsp;&nbsp;&nbsp;

ol()  and  ul()

Permits you to generate ordered or unordered HTML lists from simple or multi-dimensional arrays. Example:

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

$list = array(
            'red',
            'blue',
            'green',
            'yellow'
            );

$attributes = array(
                    'class' => 'boldlist',
                    'id'    => 'mylist'
                    );

echo ul($list, $attributes);

The above code will produce this:

<ul class="boldlist" id="mylist">
  <li>red</li>
  <li>blue</li>
  <li>green</li>
  <li>yellow</li>
</ul>

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

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

$list = array(
            'colors' => array(
                                'red',
                                'blue',
                                'green'
                            ),
            'shapes' => array(
                                'round',
                                'square',
                                'circles' => array(
                                                    'ellipse',
                                                    'oval',
                                                    'sphere'
                                                    )
                            ),
            'moods'    => array(
                                'happy',
                                'upset' => array(
                                                    'defeated' => array(
                                                                        'dejected',
                                                                        'disheartened',
                                                                        'depressed'
                                                                        ),
                                                    'annoyed',
                                                    'cross',
                                                    'angry'
                                                )
                            )
            );


echo ul($list);

The above code will produce this:

<ul class="boldlist" id="mylist">
  <li>colors
    <ul>
      <li>red</li>
      <li>blue</li>
      <li>green</li>
    </ul>
  </li>
  <li>shapes
    <ul>
      <li>round</li>
      <li>suare</li>
      <li>circles
        <ul>
          <li>elipse</li>
          <li>oval</li>
          <li>sphere</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>moods
    <ul>
      <li>happy</li>
      <li>upset
        <ul>
          <li>defeated
            <ul>
              <li>dejected</li>
              <li>disheartened</li>
              <li>depressed</li>
            </ul>
          </li>
          <li>annoyed</li>
          <li>cross</li>
          <li>angry</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>