This is documentation for Kohana v2.3.x.

Table of Contents

HTML Helper

The HTML helper assists in calling various elements such as stylesheet, javascript, image links and anchor links into position.

Methods

specialchars()

'specialchars' is similar to PHP's htmlspecialchars() function. However, there are some small differences:

The two arguments are:

Example:

$string = '<p>"I\'m hungry"&mdash;Cookie Monster said.</p>';
echo html::specialchars($string);

It will result in the following HTML:

&lt;p&gt;&quot;I&#039;m hungry&quot;&amp;mdash;Cookie Monster said.&lt;/p&gt;

When setting the second parameter to FALSE, existing HTML entities are preserved. Look closely at &mdash;.

echo html::specialchars($string, FALSE);
&lt;p&gt;&quot;I&#39;m hungry&quot;&mdash;Cookie Monster said.&lt;/p&gt;

anchor()

'anchor' creates a HTML anchor (<a href=””></a>), linking an internal page or external site automatically.

The four arguments are:

Example 1:

echo html::anchor('home/news', 'Go to our news section!');

It will result in HTML as:

<a href="http://localhost/home/news">Go to our news section!</a>

Example 2:

echo html::anchor('irc://irc.freenode.net/kohana', 'Join us on IRC!', array('style'=>'font-size: 20px;'));

It will result in HTML as:

<a href="irc://irc.freenode.net/kohana" style="font-size: 20px;">Join us on IRC!</a>

file_anchor()

Similar to 'anchor', 'file_anchor' creates a HTML anchor (<a href=””></a>) linking to non-Kohana resources. Therefore, it will always prefix the site's URL to the path of your file.

The four arguments are:

Example 1:

echo html::file_anchor('media/files/2007-12-magazine.pdf', 'Check out our latest magazine!');

It will result in HTML as:

<a href="http://localhost/media/files/2007-12-magazine.pdf">Check out our latest magazine!</a>

Example 2:

echo html::file_anchor('pub/index.php', 'The Public Linux Archive', array('id'=>'id432'), 'ftp');

It will result in HTML as:

<a href="ftp://localhost/pub/index.php" id="id432">The Public Linux Archive</a>

panchor()

Similar to 'anchor', but accepts the protocol attribute first instead of last.

The four arguments are:

Example:

echo html::panchor('irc', '/kohana', 'Join us on our custom IRC!');

It will result in HTML as:

<a href="irc://localhost/kohana">Join us on our custom IRC!</a>

anchor_array()

anchor_array($array) create an array of anchors from an array of link/title pairs. It takes:

Example:

echo Kohana::debug(html::anchor_array(array('home/news' => 'Go to our news section!', 'home/about' => 'Go to the about page')));

It will result as:

(array) Array
(
    [0] => <a href="/kohana/index.php/home/news">Go to our news section!</a>
    [1] => <a href="/kohana/index.php/home/about">Go to the about page</a>
)

email()

'email($email)' generates an obfuscated version of an email address. It escapes all characters of the e-mail address into HTML, hex or raw randomly to help prevent spam and e-mail harvesting. It takes:

Example:

echo Kohana::debug(html::email('[email protected]'));

It could result as:

(string) t&#101;&#x73;&#116;&#x40;m&#121;&#x64;o&#109;&#x61;&#105;n&#46;&#x63;o&#109;

mailto()

'mailto' prints a <a href=“mailto:”></a> tag but escapes all characters of the e-mail with the above method.

The three arguments are:

Example:

echo html::mailto('[email protected]');

It will result in HTML as:

<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;i&#x6e;fo&#x40;&#101;&#x78;&#x61;mp&#108;e&#x2e;&#x63;&#x6f;&#109;">i&#x6e;fo&#x40;&#101;&#x78;&#x61;mp&#108;e&#x2e;&#x63;&#x6f;&#109;</a>

meta()

'meta($tag, $value = NULL)' creates a meta tag.

The two arguments are:

Example:

echo html::meta('generator', 'Kohana 2.2');
echo html::meta(array('generator' => 'Kohana 2.2', 'robots' => 'noindex,nofollow'));

It will result in HTML as:

<meta name="generator" content="Kohana 2.2" />
 
<meta name="generator" content="Kohana 2.2" />
<meta name="robots" content="noindex,nofollow" />

stylesheet()

'stylesheet' calls CSS files internally and will suffix .css if it is not already present. It supports full absolute URL.

The three arguments are:

Example:

echo html::stylesheet(array
(
    'media/css/default',
    'media/css/menu',
    'http://developer.yahoo.com/yui/build/reset-fonts-grids/reset-fonts-grids.css'
),
array
(
    'screen',
    'print',
    'print'
), FALSE);

It will result in HTML as:

<link rel="stylesheet" type="text/css" href="http://localhost/media/css/default.css" media="screen" />
<link rel="stylesheet" type="text/css" href="http://localhost/media/css/menu.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://developer.yahoo.com/yui/build/reset-fonts-grids/reset-fonts-grids.css" media="print" />

Important

Don't forget to add a final TRUE parameter if your Kohana frameworks still need “index.php” in the URL (this will be the case until you modify this setting as explained in the tutorial from Christophe http://kohanaphp.com/tutorials/video/working_with_media_files.php).

link()

'link' calls files such as feeds internally. Will render the <link> tag. Linking to stylesheets also uses the <link> tag but the html::stylesheet() helper can be used for those.

Arguments:

Example:

echo html::link(array
(
    'welcome/home/rss',
    'welcome/home/atom'
),
'alternate',
array('application/rss+xml','application/atom+xml')
, FALSE);

It will result in HTML as:

<link rel="alternate" type="application/rss+xml" href="http://localhost/welcome/home/rss" />
<link rel="alternate" type="application/atom+xml" href="http://localhost/welcome/home/atom" />

Important

Don't forget to add a final TRUE parameter if your Kohana frameworks still need “index.php” in the URL (this will be the case until you modify this setting as explained in the tutorial from Christophe http://kohanaphp.com/tutorials/video/working_with_media_files.php).

script()

'script' calls JavaScript files internally and will suffix .js if not present in your file call. It supports full absolute URL.

The two arguments are:

Example:

echo html::script(array
(
    'media/js/login',
    'media/js/iefixes.js',
    'http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js'
), FALSE);

It will result in HTML as:

<script type="text/javascript" src="http://localhost/media/js/login.js"></script>
<script type="text/javascript" src="http://localhost/media/js/iefixes.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>

Important

Don't forget to add a final TRUE parameter if your Kohana frameworks still need “index.php” in the URL (this will be the case until you modify this setting as explained in the tutorial from Christophe http://kohanaphp.com/tutorials/video/working_with_media_files.php).

image()

'image' creates a 'img' HTML tag.

There are three arguments:

Example 1:

echo html::image('media/images/thumbs/01.png');

It will result in HTML as:

<img src="http://localhost/media/images/thumbs/01.png" />
echo html::image(array('src' => 'media/images/thumbs/01.png', 'width' => '100', 'height' => 100), array('alt' => 'Thumbnail', 'class' => 'noborder'));
<img src="http://localhost/media/images/thumbs/01.png" width="100" height="100" alt="Thumbnail" class="noborder"/>

Example 2 (with html::anchor and lightbox):

echo html::file_anchor('media/images/01.png', html::image('media/images/thumbs/01.png'), array('rel'=>'lightbox'));

It will result in HTML as:

<a href="http://localhost/media/images/01.png" rel="lightbox"><img src="http://localhost/media/images/thumbs/01.png" /></a>

Important

Don't forget to add a final TRUE parameter if your Kohana frameworks still need “index.php” in the URL (this will be the case until you modify this setting as explained in the tutorial from Christophe http://kohanaphp.com/tutorials/video/working_with_media_files.php).

attributes()

'attributes' parses attributes for a HTML tag from an array.

There are two arguments are:

Example 1:

echo html::attributes(
	array
	(
		'style' => 'font-size: 20px; border-bottom: 1px solid #000;',
		'rel' => 'lightbox',
		'class' => 'image'
	)
);

It will result in HTML as:

style="font-size: 20px; border-bottom: 1px solid #000;" rel="lightbox" class="image"

Example 2 (with html::anchor):

echo html::file_anchor('home/images/01.png', 'See my picture!',
html::attributes(
	array
	(
		'style' => 'font-size: 20px; border-bottom: 4px solid #000;',
		'rel' => 'lightbox',
		'class' => 'image'
	)
)
);

It will result in HTML as:

<a href="http://localhost/home/images/01.png"  style="font-size: 20px; border-bottom: 4px solid #000;" rel="lightbox" class="image">See my picture!</a>

breadcrumb()

The function returns an array of links for each segment.

Arguments:

Example:

  echo Kohana::debug(html::breadcrumb());

will produce the following output:

 Array
 (
    [0] => <a href="http://localhost/ajax">Ajax</a>
    [1] => <a href="http://localhost/ajax/welcome">Welcome</a>
    [2] => <a href="http://localhost/welcome/text">Text</a>
 )

Creating Breadcrumbs

Creating breadcrumbs is easy; use the following code as an example:

public function get_breadcrumbs()
{
	global $breadcrumbs;
 
	$get_breadcrumbs = html::breadcrumb();
	while (current($get_breadcrumbs))
	{
		$breadcrumbs .= current($get_breadcrumbs);
 
		// Check if we have reached the last crumb
		if (key($get_breadcrumbs) < (count($get_breadcrumbs)-1))
		{
			// If we haven't, add a breadcrumb separator
			$breadcrumbs .= ' / ';
		}
		next($get_breadcrumbs);
	}
		return $breadcrumbs;
}

A function like this could be included in your 'MY_Controller' library and made available to every page. Displaying the breadcrumb (ie. from a view) is as easy as:

echo $this->get_breadcrumbs();

This function will display each breadcrumb as a hyper-link. However, we may want the hyper-link removed from the last link (as we are currently on that page) and have it bold instead. This can be achieved by using this code:

public function get_breadcrumbs()
{
	global $breadcrumbs;
 
	$get_breadcrumbs = html::breadcrumb();
	while(current($get_breadcrumbs))
	{
		// Check if we have reached the last crumb
		if(key($get_breadcrumbs) < (count($get_breadcrumbs)-1))
		{
			// If we haven't, add a breadcrumb separator
			$breadcrumbs .= current($get_breadcrumbs).' / ';
		}
		else
		{
			// If we have, remove the anchor from the breadcrumb and make it bold
			$breadcrumbs .= strip_tags("<strong>".current($get_breadcrumbs)."</strong>", "<strong>");
		}
		next($get_breadcrumbs);
	}
		return $breadcrumbs;
}

Note: For simplification, the above code includes html (<strong> tags) in a library file. When implementing code like this it is best to follow conventional guidelines, such as creating <spans> and using css for styling.