-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathexample-draw-string.php
169 lines (126 loc) · 4.28 KB
/
example-draw-string.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
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
<?php
// include
require "library/Rain/autoload.php";
// namespace
use Rain\Tpl;
memoryUsageStart();
timerStart();
// include
include "library/Rain/Tpl.php";
// config
$config = array(
"base_url" => null,
"tpl_dir" => "templates/test/",
"cache_dir" => "cache/",
"debug" => true // set to false to improve the speed
);
Tpl::configure( $config );
// Add PathReplace plugin
Tpl::registerPlugin( new Tpl\Plugin\PathReplace() );
// set variables
$var = array(
"variable" => "Hello World!",
"version" => "3.0 Alpha",
"menu" => array(
array("name" => "Home", "link" => "index.php", "selected" => true ),
array("name" => "FAQ", "link" => "index.php/FAQ/", "selected" => null ),
array("name" => "Documentation", "link" => "index.php/doc/", "selected" => null )
),
"week" => array( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ),
"user" => (object) array("name"=>"Rain", "citizen" => "Earth", "race" => "Human" ),
"numbers" => array( 3, 2, 1 ),
"bad_text" => "Hey this is a malicious XSS <script>alert('auto_escape is set enabled by default, so this script is escaped!');</script>",
"table" => array( array( "Apple", "1996" ), array( "PC", "1997" ) ),
"title" => "Rain TPL 3 - Easy and Fast template engine",
"copyright" => "Copyright 2006 - 2012 Rain TPL<br>Project By Rain Team",
);
// add a tag
Tpl::registerTag( "({@.*?@})", // preg split
"{@(.*?)@}", // preg match
function( $params ){ // function called by the tag
$value = $params[1][0];
return "Translate: <b>$value</b>";
}
);
// add a tag
Tpl::registerTag( "({%.*?%})", // preg split
"{%(.*?)(?:\|(.*?))%}", // preg match
function( $params ){ // function called by the tag
$value = $params[1][0];
$value2 = $params[2][0];
return "Translate: <b>$value</b> in <b>$value2</b>";
}
);
$string = file_get_contents( "templates/test/test.html");
class Test{
static public function method( $variable ){
echo "Hi I am a static method, and this is the parameter passed to me: $variable!";
}
}
// draw
$tpl = new Tpl;
$tpl->assign( $var );
echo $tpl->drawString( $string );
// -- end
//-------------------------------------------------------------
//
// BENCHMARK/DEBUG FUNCTIONS
//
//-------------------------------------------------------------
echo "<br>---------<br>";
echo memoryUsage();
echo "<br>";
echo timer();
/**
* Useful for debug, print the variable $mixed and die
*/
function dump( $mixed, $exit = 1 ){
echo "<pre>dump \n---------------------- \n\n" . print_r( $mixed, true ) . "\n----------------------<pre>";
if( $exit ) exit;
}
/**
* Save the memory used at this point
*/
function memoryUsageStart( $memName = "execution_time" ){
return $GLOBALS['memoryCounter'][$memName] = memory_get_usage();
}
/**
* Get the memory used
*/
function memoryUsage( $memName = "execution_time", $byteFormat = true ){
$totMem = memory_get_usage() - $GLOBALS['memoryCounter'][ $memName ];
return $byteFormat ? byteFormat($totMem) : $totMem;
}
//-------------------------------------------------------------
//
// TIME FUNCTIONS
//
//-------------------------------------------------------------
/**
* Start the timer
*/
function timerStart( $timeName = "execution_time" ){
$stimer = explode( ' ', microtime( ) );
$GLOBALS['timeCounter'][$timeName] = $stimer[ 1 ] + $stimer[ 0 ];
}
/**
* Get the time passed
*/
function timer( $timeName = "execution_time", $precision = 10 ){
$etimer = explode( ' ', microtime( ) );
$timeElapsed = $etimer[ 1 ] + $etimer[ 0 ] - $GLOBALS['timeCounter'][ $timeName ];
return substr( $timeElapsed, 0, $precision );
}
/**
* Convert byte to more readable format, like "1 KB" instead of "1024".
* cut_zero, remove the 0 after comma ex: 10,00 => 10 14,30 => 14,3
*/
function byteFormat( $size ){
if( $size > 0 ){
$unim = array("B","KB","MB","GB","TB","PB");
for( $i=0; $size >= 1024; $i++ )
$size = $size / 1024;
return number_format($size,$i?2:0, ',', '.' )." ".$unim[$i];
}
}
// end