CodeIgniter: Extend HTML Helper, script_tag(); added
June 26th, 2008 | by Isern Palaus |Since pass to 1.5 to 1.6 from CodeIgniter, the framework I actually use, has added various functions with notable consequences.
- img()
Lets you create HTMLtags. The first parameter contains the image source. There is data, the second the size of the heading.
- link_tag()
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 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.
And more others with form related are the most useful for me. When I have seen what link_tag() does, I’ve think that how potential will be a function in HTML Helper that allows you to add JavaScript files. I started thinking that how this can be developed and what I did is open the existing helper, looked for link_tag() code and modified to add JavaScript files.
Now this function is full working and you can use to reduce the code on your
tags. I recommend it because is easiest to modify anything and you don’t need to write the code.It looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * Script * * Generates a script inclusion of a JavaScript file * Based on the CodeIgniters original Link Tag. * * * @access public * @param mixed javascript sources or an array * @param string language * @param string type * @param boolean should index_page be added to the javascript path * @author Isern Palaus <ipalaus@ipalaus.com> * @return string */ if ( ! function_exists('script_tag')) { function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE) { $CI =& get_instance(); $script = '<script'; if (is_array($src)) { foreach ($src as $k=>$v) { if ($k == 'src' AND strpos($v, '://') === FALSE) { if ($index_page === TRUE) { $script .= ' src="'.$CI->config->site_url($v).'"'; } else { $script .= ' src="'.$CI->config->slash_item('base_url').$v.'"'; } } else { $script .= "$k=\"$v\""; } } $script .= "></script>\n"; } else { if ( strpos($src, '://') !== FALSE) { $script .= ' src="'.$src.'" '; } elseif ($index_page === TRUE) { $script .= ' src="'.$CI->config->site_url($src).'" '; } else { $script .= ' src="'.$CI->config->slash_item('base_url').$src.'" '; } $script .= 'language="'.$language.'" type="'.$type.'"'; $script .= '></script>'."\n"; } return $script; } } ?> |
To use this code is easy, you only need to do:
<?php echo script_tag("file.js"); // OUTPUT: <script src="http://www.domain.tld/file.js" language="javascript" type="text/javascript"></script> ?>
Wish it helps.
Tags: CodeIgniter, Helper, PHP
2 Responses to “CodeIgniter: Extend HTML Helper, script_tag(); added”
By Evan on Aug 6, 2008 | Reply
Thankyou! Very handy. Definitely a glaring omission from the standard CI html helper.
By Prato on Sep 25, 2008 | Reply
Nice! I think why EllisLab’s developers didn’t think about it. This helper is very useful to everyone.