templight-02

Gesponsorde koppelingen

PHP script bestanden

  1. templight-02

« Lees de omschrijving en reacties

TempLight.php

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php

    ####
    # TempLight v0.2
    # Michel Ypma
    # Netherlands 2008
    # www.michelypma.com
    ##


    class        TempLight
    implements     ArrayAccess, Iterator, Countable
     {

//__________________________________________________________________________________________________
//                                                                                 A T T R I B U T E S


        private $__globals;                     // hold assigned variables
        private $__templateFilename;            // target template preset
        private $__templateDir;                 // target template directory
        
        private $__viewOnDestruct       = false;// view template on destruct
        private $__extractVars          = true; // extract the __globals
        private $__outputCompression    = false;// compress the output

//__________________________________________________________________________________________________
//                                                                                       S T R U C T S


        private function __construct        ( )
        {

            $this->__globals = array();
        }


        public function __destruct        ( )
        {

            if ( $this->__viewOnDestruct )
                try {
                    $this->__viewOnDestruct = false;
                    $this->view();
                }
catch ( Exception $e )
                {

                    print $e->__toString();
                }
        }


//__________________________________________________________________________________________________
//                                                                 G E T T E R S  A N D  S E T T E R S


        public function __get   ( $_key )
        {

            return $this->offsetGet ( $_key );
        }


        public function __set   ( $_key, $_value )
        {

            $this->offsetSet ( $key, $value );
        }


//__________________________________________________________________________________________________
//                                                                                   S I N G L E T O N
        /**
         * return instance of TempLight
         *
         * @return TempLight
         */

        public static function getInstance()
        {

            static $instance;

            if (! is_object ( $instance ) )
            {

                $obj = __CLASS__;
                $instance = new $obj();
            }

            return $instance;
        }


//__________________________________________________________________________________________________
//                                                                                   S H O R T C U T S


        public function add ( $_key = '', $_global )
        {

            if ( strlen ( $_key ) > 0 )
            {

                if ( ! $this->offsetExists ( $_key ) )
                {

                    $this->__globals [ $_key ] = $_global;
                }
else
                {
                    throw new Exception( 'Duplicate entry found on ' . $_key );
                }
            }
else
            {
                $this->__globals[] = $_global;
            }
        }


        public function delete ( $_key )
        {

            $this->offsetUnset ( $_key );
        }


        public function clear ()
        {

            $this->__globals = array();
        }

        
        public function get ( $_offset )
        {

            return $this->offsetGet ( $_offset );
        }


//__________________________________________________________________________________________________
//                                                                    implement: A R R A Y A C C E S S


        public function offsetExists ( $_offset )
        {

            return isset ( $this->__globals [ $_offset ] );
        }


        public function offsetGet ( $_offset )
        {

            if ( $this->offsetExists ( $_offset ) )
                return $this->__globals [ $_offset ];
            else
                throw new RangeException( 'Unknown var ' . $_offset . 'used' );
        }


        public function offsetSet ( $_offset, $_value )
        {

            if ( $this->offsetExists ( $_offset ) )
                $this->__globals [ $_offset ] = $_value;
            else
                throw new RangeException( 'Unknown var ' . $_offset . 'used' );
        }


        public function offsetUnset ( $offset )
        {

            if ( $this->offsetExists ( $offset ) )
                unset ( $this->__globals [ $offset ] );
            else
                throw new RangeException( 'Index out of bounds' );
        }


//__________________________________________________________________________________________________
//                                                                          implement: I T E R A T O R


        public function current    ()
        {

            return current ( $this->__globals );
        }


        public function next    ()
        {

            return next ( $this->__globals );
        }


        public function key        ()
        {

            return key ( $this->__globals );
        }


        public function rewind    ()
        {

            reset ( $this->__globals );
        }


        public function valid    ()
        {

            return $this->current() !== false;
        }


//__________________________________________________________________________________________________
//                                                                        implement: C O U N T A B L E


        public function count     ()
        {

            return count ( $this->__globals );
        }


//__________________________________________________________________________________________________
//                                                                                        T E M P L A T E

        /**
         * Path to your templates directory
         *
         * @param string $_path
         */

        public function setTemplateDirectory( $_path )
        {

            $this->__templateDir     = $_path;
        }

        
        /**
         * Enable output compression
         *
         * @param boolean $_bool
         */

        public function enableOutputCompression( $_bool )
        {

            $this->__outputCompression     = $_bool;
        }


        /**
         * Template filename
         *
         * @param string $_file
         */

        public function setTemplate( $_file )
        {

            $this->__templateFilename    = $_file;
        }

        
        /**
         * Automatic view template on ending
         *
         * @param boolean $_bool
         */

        public function viewOnDestruct( $_bool )
        {

            $this->__viewOnDestruct = (bool) $_bool;
        }

        
        /**
         * acces values by straight variables
         *
         * @param boolean $_bool
         */

        public function extractVars( $_bool )
        {

            $this->__extractVars = (bool) $_bool;
        }


        /**
         * send the template to the browser
         *
         * @param string $_filename
         * @return boolean
         */

        public function view( $_filename = '' )
        {

            if ( $_filename != '')
                $this->__templateFilename = $filename;
                
            if ( $this->__viewOnDestruct == true )
                throw new Exception( 'Calling TempLight::view() when viewOnDestruct is enabled isn\'t allowed' );
            
            $output = $this->getTemplateData();
            
            if ( $this->__outputCompression == true )
                $output = $this->compressOutput( $output );
            print $output;
        }


        /**
         * returns the evaluated template
         *
         * @param string $_filename
         * @return string
         */

        public function result( $_filename = '' )
        {

            if ( $_filename != '')
                $this->__templateFilename = $filename;
            
            return $this->getTemplateData();
        }

        
        private function getTemplateData()
        {

            ob_start ( );
            
            if ( $this->__extractVars )
                extract( $this->__globals );

            if ( !$this->__templateFilename )
                throw new Exception('Missing template filename');
                
            include( $this->__templateDir . $this->__templateFilename . '.php' );
            
            return ob_get_clean ( );
        }

        
//__________________________________________________________________________________________________
//                                                              A D D I T I O N A L  F E A T U R E S


        public function compressOutput( $output )
        {

            require_once('HTMLCompressor.php');
            $Compressor = new HTMLCompressor( $output, array('pre','script','textarea','code') );
            $Compressor->convert();
            return $Compressor->getOutput();
        }
    }


?>


HTMLCompression.php :
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
<?php

    ####
    # HTMLCompressor v0.1
    # Part of TempLight v0.2
    # Michel Ypma
    # Netherlands 2008
    # www.michelypma.com
    ##

    
    class HTMLCompressor {
        
//__________________________________________________________________________________________________
//                                                                                 A T T R I B U T E S

        
        private $__output;
        
        private $__preserveData        = array();
        private $__preserveBlocks    = array();
        
//__________________________________________________________________________________________________
//                                                                                       S T R U C T S

        
        public function __construct ( $_output, array $_preserveBlocks = array() )
        {

            $this->setOutput        ( $_output );
            $this->__preserveBlocks    = $_preserveBlocks;
        }

        
        public function convert ()
        {
            
            $this->__output = $this->compressHorizontally       ( );
            $this->__output = $this->compressVertically         ( );
            $this->__output = $this->stripHTMLComments            ( );
            
            return $this->__output;
        }

        
//__________________________________________________________________________________________________
//                                                                                        G E T T E R S

        
        public function getOutput()
        {

            if ( $this->__preserveBlocks )
                    $this->restorePreserves ( $block );
            return $this->__output;
        }

        
//__________________________________________________________________________________________________
//                                                                                        S E T T E R S

        
        public function setOutput( $_str )
        {

            $this->backupPreserves();
            $this->__output = $_str;
        }

        
//__________________________________________________________________________________________________
//                                                                                C O M P R E S S I O N

        
        public function compressHorizontally ()
        {

            $this->__output = preg_replace("/((?)\n)[\s]+/m", '\1', $this->__output);
            return $this->__output;
        }

        
        public function compressVertically ()
        {

            $this->__output = str_replace("\n",'', $this->__output);
            return $this->__output;
        }

        
        public function stripHTMLComments ()
        {

            $this->__output = preg_replace( '/<!--.*?-->/', '', $this->__output );
            return $this->__output;
        }

        
//__________________________________________________________________________________________________
//                                                                             P R E S E R V E  D A T A

        
        public function preserveBlock    ( $elm )
        {

            preg_match_all('#<' . $elm . '.*>(.*)<\/' . $elm . '>#ismU', $this->__output, $matches, PREG_SET_ORDER);
            $this->__output = preg_replace('#<' . $elm . '.*>(.*)<\/' . $elm . '>#ismU', '@@@'.$elm.'@@@', $this->__output );
            $this->__preserveData[ $elm ]     = $matches;
        }

        
        public function backupPreserves    ()
        {

            foreach( $this->__preserveBlocks as $block )
                $this->preserveBlock( $block );
        }

        
        public function restorePreserves ()
        {

            foreach( $this->__preserveData as $elm => $datas )
                foreach( $datas as $k => $v)
                    $this->__output = preg_replace( '#@@@'.$elm.'@@@#', $v[0], $this->__output, 1 );
        }
        
    }

?>

in je config:
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
<?php
// require TempLight
require_once ( 'TempLight.php' );

// get TempLight instance
$TempLight = TempLight::getInstance ();

// templates directory
$TempLight->setTemplateDirectory ( 'templates/' );

// extract vars?
$TempLight->extractVars ( true );

// ondestruct
$TempLight->viewOnDestruct ( true );

// output compression
$TempLight->enableOutputCompression ( true );
?>


programmeer logica:
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
<?php
// you can define the template here, it will be shown automatic on destruct
$TempLight->setTemplate ( 'index.tpl' );

/**
 * some nifty code logic here
 */

//assign a variable its value for template usage

$TempLight->add ('foo','bar');

// this won't affect your template vars... yes you're free to go in your codelogic
$foo = 'something else';

//in case you don't want to show the template on destruct you can call the view method
//$TempLight->view ();

// result will return the evaluated template
//echo $TempLight->result ();

?>


template:
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>TempLight Example</title>
</head>

<body>
<p>
$foo; geeft: <?php echo $foo; ?> als ondestruct is geconfigureerd<br />
$this['foo']; geeft: <?php echo $this['foo']; ?><br />
$this->foo; geeft: <?php echo $this->foo; ?>
</p>
</body>

</html>

 
 

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.