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:
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>
|
||||
Reference in New Issue
Block a user