====== 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 [[http://php.net/htmlspecialchars|htmlspecialchars()]] function. However, there are some small differences: * It will automatically use the UTF-8 character set in conversion (instead of ISO-8859-1). * It will automatically translate both single and double quotes to HTML entities (instead of only double quotes). * It provides built-in fallback functionality for not double encoding existing HTML entities (for PHP versions older than 5.2.3). The two arguments are: * [string] The string you want to encode * [boolean] Do you want to encode existing HTML entities? -- TRUE by default **Example:** $string = '

"I\'m hungry"—Cookie Monster said.

'; echo html::specialchars($string);
It will result in the following HTML: <p>"I'm hungry"&mdash;Cookie Monster said.</p> When setting the second parameter to FALSE, existing HTML entities are preserved. Look closely at ''—''. echo html::specialchars($string, FALSE); <p>"I'm hungry"—Cookie Monster said.</p> ==== anchor() ==== 'anchor' creates a HTML anchor (), linking an internal page or external site automatically. The four arguments are: * [string] An internal or external page that you would like to link to * [string] The title you would like to have show up as the hyperlink * [array] Attributes to add to your anchor * [string] The protocol your link will use: 'ftp', 'irc', etc. -- This is only necessary if it's an internal page with a non-absolute link for the first argument and you need to change the protocol from 'http' **Example 1:** echo html::anchor('home/news', 'Go to our news section!'); It will result in HTML as: Go to our news section! **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: Join us on IRC! ==== file_anchor() ==== Similar to 'anchor', 'file_anchor' creates a HTML anchor () linking to non-Kohana resources. Therefore, it will always prefix the site's URL to the path of your file. The four arguments are: * [string] An internal file that you would like to link to * [string] The title you would like to have show up as the hyperlink * [array] Attributes to add to your anchor * [string] The protocol your link will use: 'ftp', 'irc', etc. -- This is only necessary if you need to change the protocol from 'http' **Example 1:** echo html::file_anchor('media/files/2007-12-magazine.pdf', 'Check out our latest magazine!'); It will result in HTML as: Check out our latest magazine! **Example 2:** echo html::file_anchor('pub/index.html', 'The Public Linux Archive', array('id'=>'id432'), 'ftp'); It will result in HTML as: The Public Linux Archive ==== panchor() ==== Similar to 'anchor', but accepts the protocol attribute first instead of last. The four arguments are: * [string] The protocol your link will use: 'ftp', 'irc', etc. This is only necessary if it's an internal page with a non-absolute link for the first argument and you need to change the protocol from 'http' * [string] An internal or external page that you would like to link to * [string] The title you would like to have show up as the hyperlink * [array] Attributes to add to your anchor **Example:** echo html::panchor('irc', '/kohana', 'Join us on our custom IRC!'); It will result in HTML as: Join us on our custom IRC! ==== anchor_array() ==== ''anchor_array($array)'' create an array of anchors from an array of link/title pairs. It takes: * [array] link/title pairs **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] => Go to our news section! [1] => Go to the about page ) ==== 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: * [string] E-mail address **Example:** echo Kohana::debug(html::email('test@mydomain.com')); It could result as: (string) test@mydomain.com ==== mailto() ==== 'mailto' prints a tag but escapes all characters of the e-mail with the above method. The three arguments are: * [string] E-mail address * [string] The title you would like to have show up as the hyperlink * [string or array] Attributes to add to your anchor **Example:** echo html::mailto('info@example.com'); It will result in HTML as: info@example.com ==== meta() ==== 'meta($tag, $value = NULL)' creates a meta tag. The two arguments are: * **[string|array]** tag name, or an array of tags * **[string]** tag "content" value - default NULL * returns **[string]** the meta tag(s) **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: ==== 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: * [string or array] Either a string with the file's location or an array of files * [string or array] Media type such as 'screen', 'print' or 'aural' * [boolean] Set to TRUE if you want to have the index.php file included in the link -- This makes the difference between processing the request through Kohana (usually a media controller) or simply calling the file with an absolute path **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: 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.html]]). ==== link() ==== 'link' calls files such as feeds internally. Will render the tag. Linking to stylesheets also uses the tag but the html::stylesheet() helper can be used for those. Arguments: * [string or array] Either a string with the file's location or an array of files * [string or array] Either a string or array with values for the 'rel' attribute (e.g. stylesheet, alternate) * [string or array] Either a string or array with values for the 'type' attribute (application/rss+xml etc.) * [boolean] set to TRUE to specify the suffix of the file, defaults to FALSE * [string or array] Either a string or array with values for the 'media' attribute (print, screen etc.) * [boolean] Set to TRUE if you want to have the index.php file included in the link -- This makes the difference between processing the request through Kohana (usually a media controller) or simply calling the file with an absolute path **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: 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.html]]). ==== 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: * [string or array] Either a string with the file's location or an array of files * [boolean] Set to TRUE if you want to have the index.php file included in the link -- This makes the difference between processing the request through Kohana (usually a media controller) or simply calling the file with an absolute path **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: 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.html]]). ==== image() ==== 'image' creates a 'img' HTML tag. There are three arguments: * [string or array] A string to specify the image 'src' attribute or an array of attributes * [string or array] A string to specify the 'alt' attribute or an array of attributes * [boolean] Set to TRUE if you want to have '/index.php/' included in the link (to use views to serve images using Kohana) **Example 1:** echo html::image('media/images/thumbs/01.png'); It will result in HTML as: echo html::image(array('src' => 'media/images/thumbs/01.png', 'width' => '100', 'height' => 100), array('alt' => 'Thumbnail', 'class' => 'noborder')); Thumbnail **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: 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.html]]). ==== attributes() ==== 'attributes' parses attributes for a HTML tag from an array. There are two arguments are: * [array] An array of attributes you'd like to add to a HTML tag **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: See my picture! ==== breadcrumb() ==== The function returns an array of links for each segment. Arguments: * [array] segments to use as breadcrumbs, defaults to using Router::$segments **Example:** echo Kohana::debug(html::breadcrumb()); will produce the following output: Array ( [0] => Ajax [1] => Welcome [2] => Text ) **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("".current($get_breadcrumbs)."", ""); } next($get_breadcrumbs); } return $breadcrumbs; } **Note:** For simplification, the above code includes html ( tags) in a library file. When implementing code like this it is best to follow conventional guidelines, such as creating and using css for styling.