speedmax (owner)

Revisions

gist: 122238 Download_button fork
public
Description:
H2o filters provide a easy way to extend the template system
Public Clone URL: git://gist.github.com/122238.git
Embed All Files: show embed
1. Utilizing h2o can really DRY more the code.html #
1
2
3
4
5
6
7
8
{{ "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>
 
2. HtmlFilters.php #
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
75
76
77
78
79
80
81
82
83
<?
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;
    }
}
 
?>