Extended Kohana HTML Helper


/ Published in: PHP
Save to your folder(s)

My Kohana HTML Helper


Copy this code and paste it in your HTML
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class html extends html_Core {
  4.  
  5. public static function lianchor(array $links, $curr_check = NULL, $li_active_class = NULL, $a_active_class = NULL)
  6. {
  7. $output = null;
  8. foreach($links as $link => $name)
  9. {
  10. $li_class = null;
  11. $a_class = null;
  12.  
  13. if($link == $curr_check)
  14. {
  15. if($li_active_class != NULL)
  16. $li_class = array('class' => $li_active_class);
  17. if($a_active_class != NULL)
  18. $a_class = array('class' => $a_active_class);
  19. }
  20.  
  21. $output .= '<li'.parent::attributes($li_class).'>'.parent::anchor($link, $name, $a_class).'</li>'."\n";
  22. }
  23.  
  24. return $output;
  25. }
  26.  
  27. public static function script($script, $index = FALSE)
  28. {
  29. $compiled = '';
  30.  
  31. if (is_array($script))
  32. {
  33. foreach ($script as $name)
  34. {
  35. $compiled .= html::script($name, $index);
  36. }
  37. }
  38. else
  39. {
  40. if (strpos($script, '://') === FALSE)
  41. {
  42. // Add the suffix only when it's not already present
  43. $script = url::base((bool) $index).$script;
  44. }
  45.  
  46. if (substr_compare($script, '.js', -3, 3, FALSE) !== 0 && substr_compare($script, '/', -1, 1, FALSE) !== 0)
  47. {
  48. // Add the javascript suffix
  49. $script .= '.js';
  50. }
  51.  
  52. $compiled = '<script type="text/javascript" src="'.$script.'"></script>';
  53. }
  54.  
  55. return $compiled."\n";
  56. }
  57.  
  58. public static function image($src = NULL, $alt = NULL, $index = FALSE)
  59. {
  60. // Create attribute list
  61. $attributes = is_array($src) ? $src : array('src' => $src);
  62.  
  63. if (is_array($alt))
  64. {
  65. $attributes += $alt;
  66. }
  67. elseif ( ! empty($alt))
  68. {
  69. // Add alt to attributes
  70. $attributes['alt'] = $alt;
  71. }
  72. else
  73. {
  74. $attributes['alt'] = '';
  75. }
  76.  
  77. if (strpos($attributes['src'], '://') === FALSE)
  78. {
  79. // Make the src attribute into an absolute URL
  80. $attributes['src'] = url::base($index).$attributes['src'];
  81. }
  82.  
  83. return '<img'.html::attributes($attributes).' />';
  84. }
  85. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.