template-class

Gesponsorde koppelingen

PHP script bestanden

  1. template-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

class Awf_Template {
    
    /**
     * The default options
     *
     * @var array
     */

    protected $options =     array(
                                'template_dir' => 'templates/',
                                'extract' => true // Do you want to use $var too next to $this->var
                            );
    
    /**
     * Store variable
     *
     * @var array
     */

    protected $vars = array();
    
    /**
     * Contructor, set some options
     *
     * @param array $options
     */

    public function __construct($options = array()){
        foreach($this->options as $option_key => $value){
            if(!empty($options[$option_key])){
                $this->options[$option_key] = $options[$option_key];
            }
        }
    }


    /**
     * Set the params
     *
     * @param string $key
     * @param mixed $value
     */

    public function assign($key,$value){
        $this->vars[(string)$key] = $value;
    }

    
    /**
     * Fast assiging the variables
     *
     * @param unknown_type $key
     * @param unknown_type $value
     */

    public function __set($key,$value){        
        $this->assign($key,$value);
    }

    
    /**
     * This methods will return one variable
     *
     * @param string $key
     * @return mixed
     */

    public function get_var($key,$default=null){
        $return = $default;
        if(isset($this->vars[(string)$key])){
            $return = $this->vars[(string)$key];
        }

        return $return;
    }

    
    /**
     * Return all the variables
     *
     * @return array
     */

    public function get_vars(){
        return $this->vars;
    }

    
    /**
     * Fast syntaxis to return just one varible
     *
     * @param string $key
     * @return mixed
     */

    public function __get($key){
        return $this->get_var($key);
    }

    
    /**
     * This method fetches the template file
     *
     * @param string $file
     * @return string: the parsed file
     */

    public function fetch($file){        
        $file = $this->options['template_dir'].(string)$file;
        
        if(file_exists($file)){
            ob_start();
            if($this->options['extract']){
                extract($this->vars);
            }

            include $file;
            return ob_get_clean();
        }
else{
            throw new Exception('This ('.$file.') template file does not exists');
        }        
    }

    
    /**
     * This function will echo the parsed template
     *
     * @param string $file
     */

    public function display($file){
        echo $this->fetch($file);
    }

    
    /**
     * This makes an Awf_Template_Var of the string, so it adds some string modifiers
     *
     * @param string $string
     * @return string
     */

    public function modify($string){
        return new Awf_Template_VarModifier((string)$string);
    }    
}





class Awf_Template_VarModifier {
    
    protected $var;
    protected $key;
    
    public function __construct($var,$key=null){
        $this->var = $var;
        $this->key = $key;
    }

    
    /*
     * The modifiers must return themselves to keep chaining possible!
     *
     */

    
    public function truncate($lenght=80,$replace='...',$middle=false){
        if(strlen($this->var) > $lenght){
            if(!$middle){
                $this->var = substr($this->var,0,$lenght).$replace;
            }
else{
                $this->var = substr($this->var,0,$length/2).$replace.substr($string, -$length/2);                
            }
        }
        
        return $this;
    }

    
    public function defaultValue($default=''){
        if(empty($this->var)){
            $this->var = $default;
        }

        return $this;
    }

    
    public function add($value=''){
        $this->var .= $value;
        return $this;
    }

    
    public function entities($quotestyle=null,$charset=null,$double_encode=null){
        $this->var = htmlentities($this->var,$quotestyle,$charset,$double_encode);
        return $this;
    }

    
    public function urlencode(){
        $this->var = urlencode($this->var);
        return $this;
    }

    
    public function indent($chars=4,$char=' '){
        $this->var = preg_replace('!^!m',str_repeat($char,$chars),$this->var);
        return $this;
    }


    public function spacify($spacify_char=' '){
        $this->var = implode($spacify_char,preg_split('//', $this->var, -1, PREG_SPLIT_NO_EMPTY));
        return $this;        
    }

    
    public function __toString(){
        return $this->var;
    }
    
}

?>



Voorbeeld:
index.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
<?php
$tpl
= new Awf_Template;

$tpl->naam = 'Arian';
$tpl->tekst = 'Lorum Ipsum bla bla';

$tpl->display('template.php');

?>


templates/template.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<p>Hallo, ik heet <?php echo $naam; ?></p>
<p><?php echo $this->modify($this->tekst)->truncate(10)->spacify(); ?></p>

<p><strong>Voledige tekst</strong><?php echo $tekst; ?>
<p>Je kunt dus $this->tekst maar ook $tekst gebruiken</p>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.