mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Dev, dev and dev. Also, docs, docs and docs.
This commit is contained in:
22
benchmark/aspect/benchmark/compile.aspect.php
Normal file
22
benchmark/aspect/benchmark/compile.aspect.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
use MF\Aspect;
|
||||
require_once __DIR__ . "/../../../lib/Autoload.php";
|
||||
|
||||
Aspect::addTemplateDir(__DIR__.'/../templates');
|
||||
Aspect::setCompileDir(__DIR__.'/../compiled');
|
||||
$total = 0;
|
||||
|
||||
$t = microtime(1);
|
||||
$tpl = Aspect::compile('syntax.tpl');
|
||||
$t = microtime(1) - $t;
|
||||
var_dump("First compile: ".$t);
|
||||
|
||||
|
||||
$t = microtime(1);
|
||||
$tpl = Aspect::compile('syntax.tpl');
|
||||
$t = microtime(1) - $t;
|
||||
var_dump("Second compile: ".$t);
|
||||
|
||||
|
||||
var_dump("Pick memory: ".memory_get_peak_usage());
|
||||
?>
|
23
benchmark/aspect/benchmark/compile.smarty3.php
Normal file
23
benchmark/aspect/benchmark/compile.smarty3.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
require_once "/data/downloads/Smarty3/libs/Smarty.class.php";
|
||||
|
||||
$smarty = new Smarty();
|
||||
|
||||
$smarty->addTemplateDir(__DIR__.'/../templates');
|
||||
$smarty->setCompileDir(__DIR__.'/../compiled');
|
||||
|
||||
$t = microtime(1);
|
||||
$tpl = $smarty->createTemplate('syntax.tpl');
|
||||
/* @var Smarty_Internal_Template $tpl */
|
||||
$tpl->compileTemplateSource();
|
||||
$t = microtime(1) - $t;
|
||||
var_dump("First compile: ".$t);
|
||||
|
||||
$t = microtime(1);
|
||||
$tpl = $smarty->createTemplate('syntax.tpl');
|
||||
/* @var Smarty_Internal_Template $tpl */
|
||||
$tpl->compileTemplateSource();
|
||||
$t = microtime(1) - $t;
|
||||
var_dump("Second compile: ".$t);
|
||||
|
||||
var_dump("Pick memory: ".memory_get_peak_usage());
|
87
benchmark/aspect/benchmark/data.php
Normal file
87
benchmark/aspect/benchmark/data.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
class obj {
|
||||
public $value = "obj.value property";
|
||||
public $num = 777;
|
||||
|
||||
public function method() {
|
||||
return "object method";
|
||||
}
|
||||
|
||||
public function methodArgs($i, $j, $k, $str, $value) {
|
||||
return "object method with ars $i, $j, $k, $str, $value";
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return "object";
|
||||
}
|
||||
|
||||
public function getSelf() {
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
"title" => "syntax test page",
|
||||
|
||||
"user" => array(
|
||||
"email" => 'bzick@megagroup.ru',
|
||||
"name" => 'Ivan'
|
||||
),
|
||||
|
||||
"data" => array(
|
||||
1 => array(
|
||||
"foo" => "data.1.foo value"
|
||||
),
|
||||
2 => 4,
|
||||
4 => "data.four value",
|
||||
5 => array(
|
||||
"bar" => "data.5.baz value",
|
||||
"baz" => array(
|
||||
"foo" => "data.5.baz.foo value"
|
||||
)
|
||||
),
|
||||
6 => array(
|
||||
"foo" => "data.6.foo value"
|
||||
),
|
||||
"bar" => "data.bar value",
|
||||
"barz" => "data.barz value",
|
||||
"baz_key" => "baz",
|
||||
"foo" => array(
|
||||
"bar" => "data.foo.baz value",
|
||||
"baz" => array(
|
||||
"foo" => "data.foo.baz.foo value",
|
||||
"ls" => array(
|
||||
4 => "data.foo.baz.ls.4 value",
|
||||
5 => 555
|
||||
)
|
||||
)
|
||||
),
|
||||
"obj" => new obj(),
|
||||
"tpl_name" => 'subdir/subtpl'
|
||||
),
|
||||
|
||||
"foo_key" => "foo",
|
||||
"bar_key" => "bar",
|
||||
"barz_key" => "barz",
|
||||
"baz_key" => "baz",
|
||||
|
||||
"item" => "some item",
|
||||
"x" => 1,
|
||||
"y" => 2,
|
||||
"tpl_name" => "subtpl",
|
||||
|
||||
"contacts" => array(
|
||||
array(
|
||||
"foo" => "bar",
|
||||
"foo1" => "bar1",
|
||||
"foo2" => "bar2",
|
||||
),
|
||||
array(
|
||||
"baz" => "buh",
|
||||
"baz1" => "buh1",
|
||||
"baz2" => "buh2",
|
||||
)
|
||||
),
|
||||
"logged_in" => false
|
||||
);
|
25
benchmark/aspect/benchmark/execute.aspect.php
Normal file
25
benchmark/aspect/benchmark/execute.aspect.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
use MF\Aspect;
|
||||
require_once __DIR__ . "/../../../lib/Autoload.php";
|
||||
|
||||
Aspect::addTemplateDir(__DIR__.'/../templates');
|
||||
Aspect::setCompileDir(__DIR__.'/../compiled');
|
||||
|
||||
/*$ct = microtime(1);
|
||||
$data = Aspect::compile('syntax.tpl',0);
|
||||
$ct = microtime(1) - $ct;
|
||||
echo "\n=====================\nCompile: $ct\n";*/
|
||||
|
||||
$_data = require_once __DIR__.'/data.php';
|
||||
|
||||
$data = Aspect::fetch('syntax.tpl', $_data, 0);
|
||||
|
||||
|
||||
$dt = microtime(1);
|
||||
$data = Aspect::fetch('syntax.tpl', $_data, 0);
|
||||
$dt = microtime(1) - $dt;
|
||||
|
||||
$data = MF\Misc\Str::strip($data, true);
|
||||
echo "$data\n====\n".md5($data)."\n=====================\nDisplay: $dt\n";
|
||||
var_dump("Pick memory: ".memory_get_peak_usage());
|
||||
?>
|
23
benchmark/aspect/benchmark/execute.native.php
Normal file
23
benchmark/aspect/benchmark/execute.native.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
use MF\Aspect;
|
||||
require_once __DIR__ . "/../../../lib/Autoload.php";
|
||||
|
||||
|
||||
$tpl = require_once __DIR__.'/data.php';
|
||||
require __DIR__.'/../templates/syntax.php';
|
||||
require __DIR__.'/../templates/subdir/subtpl.php';
|
||||
|
||||
ob_start();
|
||||
template_syntax($tpl);
|
||||
$data = ob_get_clean();
|
||||
|
||||
$dt = microtime(1);
|
||||
ob_start();
|
||||
template_syntax($tpl);
|
||||
$data = ob_get_clean();
|
||||
$dt = microtime(1) - $dt;
|
||||
|
||||
$data = MF\Misc\Str::strip($data, true);
|
||||
echo "$data\n====\n".md5($data)."\n=====================\nDisplay: $dt\n";
|
||||
var_dump("Pick memory: ".memory_get_peak_usage());
|
||||
?>
|
25
benchmark/aspect/benchmark/execute.simple.aspect.php
Normal file
25
benchmark/aspect/benchmark/execute.simple.aspect.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
use MF\Aspect;
|
||||
require_once __DIR__ . "/../../../lib/Autoload.php";
|
||||
|
||||
Aspect::setTemplateDir(__DIR__.'/../templates');
|
||||
Aspect::setCompileDir(__DIR__.'/../compiled');
|
||||
|
||||
$ct = microtime(1);
|
||||
$data = Aspect::compile('simple.tpl',0);
|
||||
$ct = microtime(1) - $ct;
|
||||
|
||||
$_data = array(
|
||||
"name" => "Ivan",
|
||||
"email" => "bzick@megagroup.ru"
|
||||
);
|
||||
|
||||
$data = Aspect::fetch('simple.tpl', $_data, 0);
|
||||
|
||||
$dt = microtime(1);
|
||||
$data = Aspect::fetch('simple.tpl', $_data, 0);
|
||||
$dt = microtime(1) - $dt;
|
||||
|
||||
|
||||
echo "\n=====================\nCompile: $ct\nDisplay: $dt\n";
|
||||
?>
|
29
benchmark/aspect/benchmark/execute.simple.smarty3.php
Normal file
29
benchmark/aspect/benchmark/execute.simple.smarty3.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
require_once "/data/downloads/Smarty3/libs/Smarty.class.php";
|
||||
|
||||
$smarty = new Smarty();
|
||||
|
||||
$smarty->addTemplateDir(__DIR__.'/../templates');
|
||||
$smarty->setCompileDir(__DIR__.'/../compiled');
|
||||
|
||||
$ct = microtime(1);
|
||||
$tpl = $smarty->createTemplate('simple.tpl');
|
||||
/* @var Smarty_Internal_Template $tpl */
|
||||
$tpl->compileTemplateSource();
|
||||
$ct = microtime(1) - $ct;
|
||||
|
||||
$_data = array(
|
||||
"name" => "Ivan",
|
||||
"email" => "bzick@megagroup.ru"
|
||||
);
|
||||
|
||||
$smarty->assign($_data);
|
||||
$data = $smarty->fetch('simple.tpl');
|
||||
|
||||
$t = microtime(1);
|
||||
$smarty->assign($_data);
|
||||
$data = $smarty->fetch('simple.tpl');
|
||||
$dt = microtime(1) - $t;
|
||||
|
||||
|
||||
echo "\n=====================\nCompile: $ct\nDisplay: $dt\n";
|
29
benchmark/aspect/benchmark/execute.smarty3.php
Normal file
29
benchmark/aspect/benchmark/execute.smarty3.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
require_once "/data/downloads/Smarty3/libs/Smarty.class.php";
|
||||
require_once __DIR__ . "/../../../lib/Autoload.php";
|
||||
$smarty = new Smarty();
|
||||
|
||||
$smarty->addTemplateDir(__DIR__.'/../templates');
|
||||
$smarty->setCompileDir(__DIR__.'/../compiled');
|
||||
|
||||
/*$ct = microtime(1);
|
||||
$tpl = $smarty->createTemplate('syntax.tpl');
|
||||
/* @var Smarty_Internal_Template $tpl */
|
||||
/*$tpl->compileTemplateSource();
|
||||
$ct = microtime(1) - $ct;
|
||||
echo "\n=====================\nCompile: $ct\n";
|
||||
*/
|
||||
|
||||
$_data = require_once __DIR__.'/data.php';
|
||||
|
||||
$smarty->assign($_data);
|
||||
$data = $smarty->fetch('syntax.tpl');
|
||||
|
||||
$t = microtime(1);
|
||||
$smarty->assign($_data);
|
||||
$data = $smarty->fetch('syntax.tpl');
|
||||
$dt = microtime(1) - $t;
|
||||
|
||||
$data = MF\Misc\Str::strip($data, true);
|
||||
echo "$data\n====\n".md5($data)."\n=====================\nDisplay: $dt\n";
|
||||
var_dump("Pick memory: ".memory_get_peak_usage());
|
47
benchmark/aspect/benchmark/foreach.last.php
Normal file
47
benchmark/aspect/benchmark/foreach.last.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
$a = new ArrayIterator(str_split(str_pad("", 4, "a")));
|
||||
|
||||
$_t = null;
|
||||
$t = microtime(1);
|
||||
reset($a);
|
||||
while($v = current($a)) {
|
||||
$k = key($a);
|
||||
next($a);
|
||||
if(key($a) === null) {
|
||||
var_dump("last");
|
||||
}
|
||||
//var_dump($v);
|
||||
}
|
||||
print_r("\n\nWhile: ".(microtime(1) - $t)."\n\n");
|
||||
|
||||
$t = microtime(1);
|
||||
$c = count($a);
|
||||
foreach($a as $k => $v) {
|
||||
if(!--$c) var_dump("last");
|
||||
//var_dump($v);
|
||||
}
|
||||
|
||||
print_r("\n\nforeach + count: ".(microtime(1) - $t)."\n\n");
|
||||
|
||||
$t = microtime(1);
|
||||
reset($a);
|
||||
while(list($k, $v) = each($a)) {
|
||||
if(key($a) === null) {
|
||||
var_dump("last");
|
||||
}
|
||||
/*next($a);
|
||||
if(key($a) === null) {
|
||||
var_dump("last");
|
||||
}*/
|
||||
//var_dump($v);
|
||||
}
|
||||
print_r("\neach: ".(microtime(1) - $t)."\n\n");
|
||||
|
||||
$t = microtime(1);
|
||||
foreach($a as $k => $v) {
|
||||
//var_dump($v);
|
||||
}
|
||||
|
||||
print_r("\n\nforeach: ".(microtime(1) - $t)."\n\n");
|
||||
?>
|
94
benchmark/aspect/templates/fullsyntax.tpl
Normal file
94
benchmark/aspect/templates/fullsyntax.tpl
Normal file
@ -0,0 +1,94 @@
|
||||
{*
|
||||
data.value = "TXT value";
|
||||
data.arr.dot.4.retval = "Four";
|
||||
data.arr.dot.5 = "FiVe";
|
||||
data.arr.i = 4;
|
||||
data.arr.retval = "Retval key";
|
||||
data.set->get = "Get props";
|
||||
data.iam->getName(...) == "return Name";
|
||||
data.iam->getFormat(...)== "return Format";
|
||||
data.num = 777;
|
||||
data.k = 0;
|
||||
data.ls = array("a" => "lit A", "c" => "lit C", "d" => "lit D");
|
||||
|
||||
*}
|
||||
Hello <b>world</b>!
|
||||
My Name is {$data.value}...
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
My Name is {$data.arr.dot|json_encode|lower}...
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>, {$data.value|upper}!
|
||||
My Name is {$data.arr[dot].5|upper}...
|
||||
My Name is {$data.arr.dot[ $data.arr.i|round ]."retval"|upper}...
|
||||
My Name is {$data.arr."retval"|upper}...
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
My Name is {$data.set->get|upper}...
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
My Name is {$data.iam->getName()|upper}...
|
||||
Your Name is {$data.you->getFormat(1, 0.4, "%dth", 'grade', $data.arr[dot].5|lower)|upper}?
|
||||
Yeah
|
||||
|
||||
|
||||
Hello <b>world</b>!
|
||||
{if isset($data.iam) && !empty($data.set->get)}
|
||||
My Name is {$data.set->get}...
|
||||
{/if}
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
{if $data.num >= 5 && $data.k++ || foo($data.value) && !bar($data.num) + 3 || $data.k?}
|
||||
My Name is {$data->user|upper}...
|
||||
{/if}
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
{foreach from=$data.ls key=k item=e}
|
||||
My Name is {$e|upper} ({$k})...
|
||||
{/foreach}
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
{switch $data.num}
|
||||
{case "dotted"}
|
||||
dotted lines<br>
|
||||
{break}
|
||||
{case 777}
|
||||
numeric lines<br>
|
||||
{break}
|
||||
{case $data[arr]["dot"].4.retval|upper}
|
||||
lister<br>
|
||||
{break}
|
||||
{/switch}
|
||||
Yeah
|
||||
|
||||
Hello <b>world</b>!
|
||||
{* if !empty($data.num) *}
|
||||
{if $data.num?}
|
||||
dotted lines<br>
|
||||
{elseif $data[arr]["dot"].4.retval|lower}
|
||||
lister<br>
|
||||
{/if}
|
||||
Yeah
|
||||
|
||||
Check system variable<br/>
|
||||
Current timestamp {$aspect.now}...<br/>
|
||||
$_GET {$aspect.get.item}...<br/>
|
||||
$_POST {$aspect.post.myval|upper}...<br/>
|
||||
$_COOKIES {$aspect.cookies["uid"]}...<br/>
|
||||
$_REQUEST {$aspect.request?}...<br/>
|
||||
Consts {$data.number|round:$aspect.const.PHP_INT_MAX}...<br/>
|
||||
Ok
|
||||
|
||||
|
||||
Hello <b>world</b>!
|
||||
{for from=1 to=$data.ls|count}
|
||||
<div>Go</div>
|
||||
{/for}
|
||||
Yeah
|
17
benchmark/aspect/templates/simple.tpl
Normal file
17
benchmark/aspect/templates/simple.tpl
Normal file
@ -0,0 +1,17 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Simple template about {$name|upper}</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{if $name}
|
||||
My name is {$name}
|
||||
{else}
|
||||
I haven't name :(
|
||||
{/if}
|
||||
|
||||
Ok.
|
||||
|
||||
My email {$email}. It is great!
|
||||
</body>
|
||||
</html>
|
18
benchmark/aspect/templates/subdir/subtpl.php
Normal file
18
benchmark/aspect/templates/subdir/subtpl.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
function template_subtpl($tpl) {
|
||||
|
||||
if($tpl["user"]["name"]) {
|
||||
echo 'My name is '.$tpl["user"]["name"];
|
||||
} else {
|
||||
echo 'I haven\'t name :(';
|
||||
};
|
||||
?>
|
||||
|
||||
Ok.
|
||||
|
||||
My email <?php echo $tpl["user"]["name"].'. It is great!'; ?>
|
||||
|
||||
<?php
|
||||
}
|
||||
?>
|
9
benchmark/aspect/templates/subdir/subtpl.tpl
Normal file
9
benchmark/aspect/templates/subdir/subtpl.tpl
Normal file
@ -0,0 +1,9 @@
|
||||
{if $user.name}
|
||||
My name is {$user.name}
|
||||
{else}
|
||||
I haven't name :(
|
||||
{/if}
|
||||
|
||||
Ok.
|
||||
|
||||
My email {$user.name}. It is great!
|
184
benchmark/aspect/templates/syntax.php
Normal file
184
benchmark/aspect/templates/syntax.php
Normal file
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
function template_syntax($tpl) {
|
||||
|
||||
?><html>
|
||||
<head>
|
||||
<title><?php echo $tpl["title"]; ?></title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
Simple manipulations
|
||||
|
||||
<?php echo $tpl["item"]; ?>
|
||||
<?php echo $tpl["data"][4]; ?>
|
||||
<?php echo $tpl["data"]["bar"]; ?>
|
||||
<?php echo $tpl["data"]["bar"]; ?>
|
||||
<?php echo $tpl["data"]["bar"]; ?>
|
||||
<?php echo $tpl["data"]['bar']; ?>
|
||||
<?php echo $tpl["data"][ $tpl["bar_key"] ]; ?>
|
||||
<?php echo $tpl["data"]["obj"]->value; ?>
|
||||
<?php echo $tpl["data"]["obj"]->method(); ?>
|
||||
|
||||
Many other combinations are allowed
|
||||
|
||||
<?php echo $tpl["data"]["foo"]["bar"]; ?>
|
||||
<?php echo $tpl["data"]["foo"][ $tpl["baz_key"] ][ $tpl["foo_key"] ]; ?>
|
||||
<?php echo $tpl["data"]["foo"][ $tpl["bar_key"] ]; ?>
|
||||
<?php echo $tpl["data"][ $tpl["foo_key"] ]["bar"]; ?>
|
||||
<?php echo $tpl["data"][5]["bar"]; ?>
|
||||
<?php echo $tpl["data"][5][ $tpl["bar_key"] ]; ?>
|
||||
<?php echo $tpl["data"]["foo"][ $tpl["baz_key"] ]["ls"][4]; ?>
|
||||
<?php echo $tpl["data"]["obj"]->methodArgs($tpl["baz_key"], 2, 2.3, "some string", $tpl["bar_key"]); ?> <-- passing parameters
|
||||
|
||||
|
||||
Math and embedding tags:
|
||||
|
||||
<?php echo $tpl["x"] + $tpl["y"]; ?> // will output the sum of x and y.
|
||||
<?php echo $tpl["data"][$tpl["x"] + 3]; ?>
|
||||
<?php echo $tpl["item"] = 4 + 3; ?> // tags within tags
|
||||
<?php echo $tpl["data"][4] = 4 + 3; ?>
|
||||
<?php echo $tpl["item"] = "this is message"; ?> // tags within double quoted strings
|
||||
|
||||
|
||||
Short variable assignment:
|
||||
|
||||
<?php echo $tpl["item"] = $tpl["y"] + 2; ?>
|
||||
<?php echo $tpl["item"] = strlen($tpl["bar_key"]); ?> // function in assignment
|
||||
<?php echo $tpl["item"] = intval(($tpl["x"] + $tpl["y"]) * 3); ?> // as function parameter
|
||||
<?php echo $tpl["data"]["bar"] = 1; ?> // assign to specific array element
|
||||
<?php echo $tpl["data"]["foo"]["bar"] = "data.foo.bar: tpl value"; ?>
|
||||
|
||||
Smarty "dot" syntax (note: embedded {} are used to address ambiguities):
|
||||
|
||||
<?php echo $tpl["data"]["foo"]["baz"]["foo"]; ?>
|
||||
<?php echo $tpl["data"]["foo"][ $tpl["baz_key"] ]["foo"]; ?>
|
||||
<?php echo $tpl["data"][$tpl["y"] + 4]["foo"]; ?>
|
||||
<?php echo $tpl["data"]["foo"][$tpl["data"]["baz_key"]]["foo"]; ?>
|
||||
|
||||
Object chaining:
|
||||
|
||||
<?php echo $tpl["data"]["obj"]->getSelf($tpl["x"])->method($tpl["y"]); ?>
|
||||
|
||||
Direct PHP function access:
|
||||
|
||||
<?php echo strlen("hi!"); ?>
|
||||
|
||||
|
||||
<?php
|
||||
if($tpl["logged_in"]) {
|
||||
echo 'Welcome, <span style="color: '.$tpl["fontColor"].'">'.$tpl["name"].'</span>';
|
||||
} else {
|
||||
echo "hi, ".$tpl["user"]["name"];
|
||||
}
|
||||
?>
|
||||
|
||||
Embedding Vars in Double Quotes
|
||||
|
||||
<?php echo MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "test ".$tpl["item"]." test"), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "test ".$tpl["foo_key"]." test"), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "test ".($tpl["data"][4])." test"), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "test ".$tpl["item"].".bar test"), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => 'test {$data.barz} test'), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "test ".($tpl["data"]["barz"])." test"), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => strtoupper("test ".($tpl["data"]["barz"])." test")), $tpl).
|
||||
MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "test ".(strtoupper($tpl["data"]["barz"]))." test"), $tpl); ?>
|
||||
|
||||
|
||||
will replace $tpl_name with value
|
||||
<?php template_subtpl($tpl); ?>
|
||||
|
||||
does NOT replace $tpl_name
|
||||
<?php echo MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => "one,two"), $tpl); ?>
|
||||
|
||||
<?php template_subtpl($tpl); ?>
|
||||
|
||||
Math
|
||||
|
||||
some more complicated examples
|
||||
|
||||
<?php echo $tpl["data"][2] - $tpl["data"]["obj"]->num * !$tpl["data"]["foo"]["baz"]["ls"][4] - 3 * 7 % $tpl["data"][2]; ?>
|
||||
|
||||
<?php
|
||||
if($tpl["data"][2] - $tpl["data"]["obj"]->num * $tpl["data"]["foo"]["baz"]["ls"][4] - 3 * 7 % $tpl["data"][2]) {
|
||||
echo MF\Misc\Str::truncate($tpl["data"]["barz"], "".($tpl["data"][2] / 2 - 1)."")."\n".
|
||||
MF\Misc\Str::truncate($tpl["data"]["barz"], ($tpl["data"][2] / 2 - 1));
|
||||
}
|
||||
?>
|
||||
|
||||
Escaping Smarty Parsing
|
||||
|
||||
<script>
|
||||
// the following braces are ignored by Smarty
|
||||
// since they are surrounded by whitespace
|
||||
function foobar() {
|
||||
alert('foobar!');
|
||||
}
|
||||
// this one will need literal escapement
|
||||
<?php ?>
|
||||
function bazzy() {alert('foobar!');}
|
||||
</script>
|
||||
|
||||
|
||||
name: <?php echo $tpl["user"]["name"]; ?><br />
|
||||
email: <?php echo $tpl["user"]["email"]; ?><br />
|
||||
|
||||
Modifier examples
|
||||
|
||||
apply modifier to a variable
|
||||
<?php echo strtoupper($tpl["user"]["name"]); ?>
|
||||
|
||||
modifier with parameters
|
||||
<?php echo MF\Misc\Str::truncate($tpl["user"]["name"], 40, "..."); ?>
|
||||
|
||||
apply modifier to a function parameter
|
||||
<?php echo MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => strtoupper($tpl["user"]["name"])), $tpl); ?>
|
||||
|
||||
with parameters
|
||||
<?php echo MF\Aspect\Func::mailto(array("address" => $tpl["user"]["email"],"text" => MF\Misc\Str::truncate($tpl["user"]["name"], 40, "...")), $tpl); ?>
|
||||
|
||||
apply modifier to literal string
|
||||
|
||||
using date_format to format the current date
|
||||
<?php echo MF\Aspect\Modifier::dateFormat(time(), "%Y/%m/%d"); ?>
|
||||
|
||||
apply modifier to a custom function
|
||||
|
||||
Foreach
|
||||
<?php
|
||||
if($tpl["contacts"]) {
|
||||
foreach($tpl["contacts"] as $tpl["contact"]) {
|
||||
if($tpl["contact"]) {
|
||||
foreach($tpl["contact"] as $tpl["key"] => $tpl["value"]) {
|
||||
echo $tpl["key"].": ".$tpl["value"]." ";
|
||||
}
|
||||
} else {
|
||||
echo "no items";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
If condition
|
||||
<?php
|
||||
if(isset($tpl["user"]["name"]) && $tpl["user"]["name"] == 'yandex') {
|
||||
echo "do something";
|
||||
} elseif($tpl["user"]["name"] == $tpl["data"]["foo"]["bar"]) {
|
||||
echo "do something2";
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
<?php
|
||||
if(is_array($tpl["data"]["foo"]) && count($tpl["data"]["foo"]) > 0) {
|
||||
echo "do a foreach loop";
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
}
|
||||
?>
|
179
benchmark/aspect/templates/syntax.tpl
Normal file
179
benchmark/aspect/templates/syntax.tpl
Normal file
@ -0,0 +1,179 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>{$title}</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{* this is a comment *}
|
||||
|
||||
Simple manipulations
|
||||
|
||||
{$item}
|
||||
{$data[4]}
|
||||
{$data.bar}
|
||||
{*$data[bar]*}
|
||||
{$data["bar"]}
|
||||
{$data['bar']}
|
||||
{$data.$bar_key}
|
||||
{$data.obj->value}
|
||||
{$data.obj->method()}
|
||||
|
||||
Many other combinations are allowed
|
||||
|
||||
{$data.foo.bar}
|
||||
{$data.foo.$baz_key.$foo_key}
|
||||
{$data.foo.$bar_key}
|
||||
{$data.$foo_key.bar}
|
||||
{$data[5].bar}
|
||||
{$data[5].$bar_key}
|
||||
{$data.foo.$baz_key.ls[4]}
|
||||
{$data.obj->methodArgs($baz_key, 2, 2.3, "some string", $bar_key)} <-- passing parameters
|
||||
{*"foo"*}
|
||||
|
||||
|
||||
Math and embedding tags:
|
||||
|
||||
{$x+$y} // will output the sum of x and y.
|
||||
{$data[$x+3]}
|
||||
{$item=4+3} // tags within tags
|
||||
{$data[4]=4+3}
|
||||
{$item="this is message"} // tags within double quoted strings
|
||||
|
||||
|
||||
Short variable assignment:
|
||||
|
||||
{$item=$y+2}
|
||||
{$item = strlen($bar_key)} // function in assignment
|
||||
{$item = intval( ($x+$y)*3 )} // as function parameter
|
||||
{$data.bar=1} // assign to specific array element
|
||||
{$data.foo.bar="data.foo.bar: tpl value"}
|
||||
|
||||
Smarty "dot" syntax (note: embedded {} are used to address ambiguities):
|
||||
|
||||
{$data.foo.baz.foo}
|
||||
{$data.foo.$baz_key.foo}
|
||||
{$data[$y+4].foo}
|
||||
{$data.foo[$data.baz_key].foo}
|
||||
|
||||
Object chaining:
|
||||
|
||||
{$data.obj->getSelf($x)->method($y)}
|
||||
|
||||
Direct PHP function access:
|
||||
|
||||
{strlen("hi!")}
|
||||
|
||||
|
||||
{if $logged_in}
|
||||
Welcome, <span style="color:{$fontColor}">{$name}!</span>
|
||||
{else}
|
||||
hi, {$user.name}
|
||||
{/if}
|
||||
|
||||
Embedding Vars in Double Quotes
|
||||
|
||||
{mailto address=$user.email text="test $item test"}
|
||||
{mailto address=$user.email text="test $foo_key test"}
|
||||
{mailto address=$user.email text="test {$data[4]} test"}
|
||||
{*mailto address=$user.email text="test {$data[barz]} test"*}
|
||||
{mailto address=$user.email text="test $item.bar test"}
|
||||
{mailto address=$user.email text='test {$data.barz} test'}
|
||||
{mailto address=$user.email text="test {$data.barz} test"}
|
||||
{mailto address=$user.email text="test {$data.barz} test"|upper}
|
||||
{mailto address=$user.email text="test {$data.barz|upper} test"}
|
||||
|
||||
|
||||
will replace $tpl_name with value
|
||||
{include file="subdir/$tpl_name.tpl"}
|
||||
|
||||
does NOT replace $tpl_name
|
||||
{mailto address=$user.email text="one,two"}
|
||||
|
||||
{include file="{$data.tpl_name}.tpl"}
|
||||
|
||||
Math
|
||||
|
||||
some more complicated examples
|
||||
|
||||
{$data[2] - $data.obj->num * !$data.foo.baz.ls[4] - 3 * 7 % $data[2]}
|
||||
|
||||
{if $data[2] - $data.obj->num * $data.foo.baz.ls[4] - 3 * 7 % $data[2]}
|
||||
{$data.barz|truncate:"{$data[2]/2-1}"}
|
||||
{$data.barz|truncate:($data[2]/2-1)}
|
||||
{/if}
|
||||
|
||||
Escaping Smarty Parsing
|
||||
|
||||
<script>
|
||||
// the following braces are ignored by Smarty
|
||||
// since they are surrounded by whitespace
|
||||
function foobar() {
|
||||
alert('foobar!');
|
||||
}
|
||||
// this one will need literal escapement
|
||||
{literal}
|
||||
function bazzy() {alert('foobar!');}
|
||||
{/literal}
|
||||
</script>
|
||||
|
||||
|
||||
name: {$user.name}<br />
|
||||
email: {$user.email}<br />
|
||||
|
||||
Modifier examples
|
||||
|
||||
apply modifier to a variable
|
||||
{$user.name|upper}
|
||||
|
||||
modifier with parameters
|
||||
{$user.name|truncate:40:"..."}
|
||||
|
||||
apply modifier to a function parameter
|
||||
{mailto address=$user.email text=$user.name|upper}
|
||||
|
||||
with parameters
|
||||
{mailto address=$user.email text=$user.name|truncate:40:"..."}
|
||||
|
||||
apply modifier to literal string
|
||||
{*"foobar"|upper*}
|
||||
|
||||
using date_format to format the current date
|
||||
{$smarty.now|date_format:"%Y/%m/%d"}
|
||||
|
||||
apply modifier to a custom function
|
||||
{*mailto|upper address="smarty@example.com"*}
|
||||
|
||||
Foreach
|
||||
{foreach $contacts as $contact}
|
||||
{foreach $contact as $key => $value}
|
||||
{$key}: {$value}
|
||||
{foreachelse}
|
||||
no items
|
||||
{/foreach}
|
||||
{/foreach}
|
||||
|
||||
If condition
|
||||
{if isset($user.name) && $user.name == 'yandex'}
|
||||
do something
|
||||
{elseif $user.name == $data.foo.bar}
|
||||
do something2
|
||||
{/if}
|
||||
|
||||
{*switch $item}
|
||||
{case 1}
|
||||
item 1
|
||||
{break}
|
||||
{case 2}
|
||||
item 2
|
||||
{break}
|
||||
{default}
|
||||
on item
|
||||
{/switch*}
|
||||
|
||||
{if is_array($data.foo) && count($data.foo) > 0}
|
||||
do a foreach loop
|
||||
{/if}
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
103
benchmark/aspect/test.php
Normal file
103
benchmark/aspect/test.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
use MF\Misc\Tokenizer;
|
||||
require_once __DIR__ . "/../../lib/Autoload.php";
|
||||
|
||||
$tokens = new \MF\Misc\Tokenizer('$data.value[5].dot|json_decode|lower');
|
||||
|
||||
|
||||
$tpl[0] = 'Hello <b>world</b>!
|
||||
My Name is {$data.value}...
|
||||
Yeah';
|
||||
|
||||
$tpl[1] = 'Hello <b>world</b>!
|
||||
My Name is {$data.value[label].dot|json_decode|lower|truncate:80:"...":true:null:0.22:$a.keyz|upper}...
|
||||
Yeah';
|
||||
|
||||
$tpl[2] = 'Hello <b>world</b>, {$user.name|upper}!
|
||||
My Name is {$data.user1.5.$i."return"[ $hello.world|lower ]|upper}...
|
||||
Yeah';
|
||||
|
||||
$tpl[3] = 'Hello <b>world</b>!
|
||||
My Name is {$data->set->get|upper}...
|
||||
Yeah';
|
||||
|
||||
$tpl[4] = 'Hello <b>world</b>!
|
||||
My Name is {$iam->getName()|upper}...
|
||||
Your Name is {$you->getFromat(1, 0.4, "%dth", \'grade\', $global->name.k|lower)|upper}?
|
||||
Yeah';
|
||||
|
||||
$tpl[5] = 'Hello <b>world</b>!
|
||||
{if isset($data.user) && !empty($data->value)}
|
||||
My Name is {$data->user|upper}...
|
||||
{/if}
|
||||
Yeah';
|
||||
|
||||
$tpl[6] = 'Hello <b>world</b>!
|
||||
{if $data.user >= 5 && $k++ || foo($data->value) && !bar($d) + 3 || $show.go?}
|
||||
My Name is {$data->user|upper}...
|
||||
{/if}
|
||||
Yeah';
|
||||
|
||||
$tpl[7] = 'Hello <b>world</b>!
|
||||
{foreach from=$users."list" key=k item=e}
|
||||
My Name is {$e|upper} ({$k})...
|
||||
{/foreach}
|
||||
Yeah';
|
||||
|
||||
$tpl[8] = 'Hello <b>world</b>!
|
||||
{switch $data->enum}
|
||||
{case "dotted"}
|
||||
dotted lines<br>
|
||||
{break}
|
||||
{case 7}
|
||||
numeric lines<br>
|
||||
{break}
|
||||
{case $list|truncate}
|
||||
lister<br>
|
||||
{break}
|
||||
{/switch}
|
||||
Yeah';
|
||||
|
||||
$tpl[9] = 'Hello <b>world</b>!
|
||||
{if $data->enum?}
|
||||
dotted lines<br>
|
||||
{elseif $list|truncate}
|
||||
lister<br>
|
||||
{/if}
|
||||
Yeah';
|
||||
|
||||
$tpl[10] = 'Check system variable<br/>
|
||||
Current timestamp {$aspect.now}...<br/>
|
||||
$_GET {$aspect.get.item}...<br/>
|
||||
$_POST {$aspect.post.myval|upper}...<br/>
|
||||
$_COOKIES {$aspect.cookies["uid"]}...<br/>
|
||||
$_REQUEST {$aspect.request?}...<br/>
|
||||
Consts {$number|round:$aspect.const.PHP_INT_MAX|}...<br/>
|
||||
Ok';
|
||||
|
||||
$tpl[11] = 'Hello <b>world</b>!
|
||||
{for from=1 to=$e|count}
|
||||
<div>Go</div>
|
||||
{/for}
|
||||
Yeah';
|
||||
|
||||
$tpl_ = '
|
||||
Hello <b>world</b>!
|
||||
My Name is {$data|upper}...
|
||||
<script>$.dotted({
|
||||
items: "sorted"
|
||||
});</script> {* comment *}
|
||||
I have
|
||||
<ul>
|
||||
{foreach $items as $key => $item}
|
||||
<li>{$item}</li>
|
||||
{/foreach}
|
||||
</ul>
|
||||
';
|
||||
|
||||
$template = new MF\Aspect\Template($tpl[7], 'my.tpl');
|
||||
//var_dump(Tokenizer::decode('case 6:'));
|
||||
//exit;
|
||||
echo "\n".$template->getBody()."\n";
|
||||
//var_dump($template->fetch(array("data" => array("value" => "Yohoho"))));
|
||||
?>
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
require_once __DIR__.'/scripts/bootstrap.php';
|
||||
exec("rm -rf ".__DIR__."/compile/*");
|
||||
|
||||
echo "Smarty3 vs Twig vs Aspect\n\n";
|
||||
|
||||
@ -7,14 +9,23 @@ passthru("php ".__DIR__."/templates/inheritance/smarty.gen.php");
|
||||
passthru("php ".__DIR__."/templates/inheritance/twig.gen.php");
|
||||
echo "Done\n";
|
||||
|
||||
echo "Testing large output...\n";
|
||||
passthru("php ".__DIR__."/templates/echo.php");
|
||||
echo "Testing a lot output...\n";
|
||||
|
||||
Benchmark::runs("smarty3", 'echo/smarty.tpl', __DIR__.'/templates/echo/data.json');
|
||||
Benchmark::runs("twig", 'echo/twig.tpl', __DIR__.'/templates/echo/data.json');
|
||||
Benchmark::runs("aspect", 'echo/smarty.tpl', __DIR__.'/templates/echo/data.json');
|
||||
|
||||
echo "\nTesting 'foreach' of big array...\n";
|
||||
passthru("php ".__DIR__."/templates/foreach.php");
|
||||
|
||||
Benchmark::runs("smarty3", 'foreach/smarty.tpl', __DIR__.'/templates/foreach/data.json');
|
||||
Benchmark::runs("twig", 'foreach/twig.tpl', __DIR__.'/templates/foreach/data.json');
|
||||
Benchmark::runs("aspect", 'foreach/smarty.tpl', __DIR__.'/templates/foreach/data.json');
|
||||
|
||||
echo "\nTesting deep 'inheritance'...\n";
|
||||
passthru("php ".__DIR__."/templates/inheritance.php");
|
||||
|
||||
Benchmark::runs("smarty3", 'inheritance/smarty/b100.tpl', __DIR__.'/templates/foreach/data.json');
|
||||
Benchmark::runs("twig", 'inheritance/twig/b100.tpl', __DIR__.'/templates/foreach/data.json');
|
||||
Benchmark::runs("aspect", 'inheritance/smarty/b100.tpl', __DIR__.'/templates/foreach/data.json');
|
||||
|
||||
echo "\nDone. Cleanup.\n";
|
||||
passthru("rm -rf ".__DIR__."/compile/*");
|
||||
|
68
benchmark/scripts/bootstrap.php
Normal file
68
benchmark/scripts/bootstrap.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
require(__DIR__.'/../../vendor/autoload.php');
|
||||
|
||||
class Benchmark {
|
||||
public static $t = "%8s: %-22s %10.4f sec, %10.1f MiB\n";
|
||||
|
||||
public static function smarty3($tpl, $data, $double, $message) {
|
||||
$smarty = new Smarty();
|
||||
$smarty->compile_check = false;
|
||||
|
||||
$smarty->setTemplateDir(__DIR__.'/../templates');
|
||||
$smarty->setCompileDir(__DIR__."/../compile/");
|
||||
|
||||
if($double) {
|
||||
$smarty->assign($data);
|
||||
$smarty->fetch($tpl);
|
||||
}
|
||||
|
||||
$start = microtime(true);
|
||||
$smarty->assign($data);
|
||||
$smarty->fetch($tpl);
|
||||
|
||||
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
|
||||
}
|
||||
|
||||
public static function twig($tpl, $data, $double, $message) {
|
||||
|
||||
Twig_Autoloader::register();
|
||||
$loader = new Twig_Loader_Filesystem(__DIR__.'/../templates');
|
||||
$twig = new Twig_Environment($loader, array(
|
||||
'cache' => __DIR__."/../compile/",
|
||||
'autoescape' => false,
|
||||
'auto_reload' => false,
|
||||
));
|
||||
|
||||
if($double) {
|
||||
$twig->loadTemplate($tpl)->render($data);
|
||||
}
|
||||
|
||||
$start = microtime(true);
|
||||
$twig->loadTemplate($tpl)->render($data);
|
||||
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
|
||||
}
|
||||
|
||||
public static function aspect($tpl, $data, $double, $message) {
|
||||
|
||||
$aspect = Aspect::factory(__DIR__.'/../templates', __DIR__."/../compile/");
|
||||
|
||||
if($double) {
|
||||
$aspect->fetch($tpl, $data);
|
||||
}
|
||||
$start = microtime(true);
|
||||
$aspect->fetch($tpl, $data);
|
||||
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
|
||||
}
|
||||
|
||||
public static function run($engine, $template, $data, $double, $message) {
|
||||
passthru(sprintf("php %s/run.php --engine '%s' --template '%s' --data '%s' --message '%s' %s", __DIR__, $engine, $template, $data, $message, $double ? '--double' : ''));
|
||||
}
|
||||
|
||||
public static function runs($engine, $template, $data) {
|
||||
self::run($engine, $template, $data, false, '!compiled and !loaded');
|
||||
self::run($engine, $template, $data, false, 'compiled and !loaded');
|
||||
self::run($engine, $template, $data, true, 'compiled and loaded');
|
||||
echo "\n";
|
||||
}
|
||||
}
|
15
benchmark/scripts/run.php
Normal file
15
benchmark/scripts/run.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$opt = getopt("", array(
|
||||
"engine:",
|
||||
"template:",
|
||||
"data:",
|
||||
"double",
|
||||
"message:"
|
||||
));
|
||||
|
||||
require_once __DIR__.'/bootstrap.php';
|
||||
|
||||
extract($opt);
|
||||
|
||||
Benchmark::$engine($template, json_decode(file_get_contents($data), true), isset($double), $message);
|
@ -1,8 +1,23 @@
|
||||
<?php
|
||||
$data = json_decode(file_get_contents(__DIR__.'/echo/data.json'), true);
|
||||
|
||||
//$data = json_decode(file_get_contents(__DIR__.'/echo/data.json'), true);
|
||||
require_once __DIR__.'/../scripts/bootstrap.php';
|
||||
exec("rm -rf ".__DIR__."/../compile/*");
|
||||
|
||||
echo "A lot outputs...\n";
|
||||
|
||||
Benchmark::run("smarty3", 'echo/smarty.tpl', __DIR__.'/echo/data.json', false, '!compiled and !loaded');
|
||||
Benchmark::run("smarty3", 'echo/smarty.tpl', __DIR__.'/echo/data.json', false, 'compiled and !loaded');
|
||||
Benchmark::run("smarty3", 'echo/smarty.tpl', __DIR__.'/echo/data.json', true, 'compiled and loaded');
|
||||
|
||||
Benchmark::run("twig", 'echo/twig.tpl', __DIR__.'/echo/data.json', false, '!compiled and !loaded');
|
||||
Benchmark::run("twig", 'echo/twig.tpl', __DIR__.'/echo/data.json', false, 'compiled and !loaded');
|
||||
Benchmark::run("twig", 'echo/twig.tpl', __DIR__.'/echo/data.json', true, 'compiled and loaded');
|
||||
|
||||
Benchmark::run("aspect", 'echo/smarty.tpl', __DIR__.'/echo/data.json', false, '!compiled and !loaded');
|
||||
Benchmark::run("aspect", 'echo/smarty.tpl', __DIR__.'/echo/data.json', false, 'compiled and !loaded');
|
||||
Benchmark::run("aspect", 'echo/smarty.tpl', __DIR__.'/echo/data.json', true, 'compiled and loaded');
|
||||
exit;
|
||||
|
||||
require(__DIR__.'/../../vendor/autoload.php');
|
||||
$smarty = new Smarty();
|
||||
$smarty->compile_check = false;
|
||||
|
@ -5,7 +5,7 @@ exec("rm -rf ".__DIR__."/../compile/*");
|
||||
|
||||
require(__DIR__.'/../../vendor/autoload.php');
|
||||
$smarty = new Smarty();
|
||||
$smarty->compile_check = false;
|
||||
$smarty->compile_check = true;
|
||||
|
||||
$smarty->setTemplateDir(__DIR__);
|
||||
$smarty->setCompileDir(__DIR__."/../compile/");
|
||||
|
@ -16,7 +16,7 @@ exec("rm -rf ".__DIR__."/../compile/*");
|
||||
require(__DIR__.'/../../vendor/autoload.php');
|
||||
|
||||
$smarty = new Smarty();
|
||||
$smarty->compile_check = false;
|
||||
$smarty->compile_check = true;
|
||||
|
||||
$smarty->setTemplateDir(__DIR__);
|
||||
$smarty->setCompileDir(__DIR__."/../compile/");
|
||||
|
Reference in New Issue
Block a user