Created
          June 2, 2009 13:37 
        
      - 
      
- 
        Save speedmax/122238 to your computer and use it in GitHub Desktop. 
    H2o filters provide a easy way to extend the template system
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | {{ "Add user"|links_to '/user/add', title: "Create a new user" }} | |
| {{ "Add Discount|links_to '/discount/add', class: "button", style: "float:right" }} | |
| instead of | |
| <a href="{{site_dir}}user/add">Add User</a><br> | |
| <a href="{{site_dir}}user/listAll">List Users</a><br><br> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <? | |
| h2o::addFilter('CustomHtmlFilters') | |
| class CustomHtmlFilters extends FilterCollection { | |
| static $base_url = ''; // Add a base url to your application | |
| static function base_url($url, $options = array()) { | |
| return self::$base_url . $url; | |
| } | |
| static function links_to($text, $url, $options = array()) { | |
| $attrs = self::htmlAttribute(array('ref'), $options); | |
| $url = self::base_url($url, $options); | |
| return sprintf('<a href="%s" %s>%s</a>', $url, $attrs, $text); | |
| } | |
| static function links_with($url, $text, $options = array()) { | |
| return self::links_to($text, $url, $options); | |
| } | |
| static function asset_url($url, $options = array()) { | |
| return self::base_url($url, $options); | |
| } | |
| static function image_tag($url, $options = array()) { | |
| $attr = self::htmlAttribute(array('alt','width','height','border'), $options); | |
| return sprintf('<img src="%s" %s/>', $url, $attr); | |
| } | |
| static function css_tag($url, $options = array()) { | |
| $attr = self::htmlAttribute(array('media'), $options); | |
| return sprintf('<link rel="stylesheet" href="%s" type="text/css" %s />', $url, $attr); | |
| } | |
| static function script_tag($url, $options = array()) { | |
| return sprintf('<script src="%s" type="text/javascript"></script>', $url); | |
| } | |
| static function strip_tags($text) { | |
| $text = preg_replace(array('/</', '/>/'), array(' <', '> '),$text); | |
| return strip_tags($text); | |
| } | |
| static function linebreaks($value, $format = 'p') { | |
| if ($format === 'br') | |
| return HtmlFilters::nl2br($value); | |
| return HtmlFilters::nl2pbr($value); | |
| } | |
| static function nl2br($value) { | |
| return str_replace("\n", "<br />\n", $value); | |
| } | |
| static function nl2pbr($value) { | |
| $result = array(); | |
| $parts = preg_split('/(\r?\n){2,}/m', $value); | |
| foreach ($parts as $part) { | |
| array_push($result, '<p>' . HtmlFilters::nl2br($part) . '</p>'); | |
| } | |
| return implode("\n", $result); | |
| } | |
| protected static function htmlAttribute($attrs = array(), $data = array()) { | |
| $attrs = self::extract(array_merge(array('id', 'class', 'title', "style"), $attrs), $data); | |
| $result = array(); | |
| foreach ($attrs as $name => $value) { | |
| $result[] = "{$name}=\"{$value}\""; | |
| } | |
| return join(' ', $result); | |
| } | |
| protected static function extract($attrs = array(), $data=array()) { | |
| $result = array(); | |
| if (empty($data)) return array(); | |
| foreach($data as $k => $e) { | |
| if (in_array($k, $attrs)) $result[$k] = $e; | |
| } | |
| return $result; | |
| } | |
| } | |
| ?> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment