From 1619126d7d87538082dd58645a37f528adb1deeb Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Fri, 23 Sep 2011 22:24:16 +0200 Subject: Added short syntag for comparison. ~{a|op|b} Also updated the documentation and made unescape_text() a alias of PHP's stripcslashes(). --- docu/language_definition.html | 20 +++++++++++++++++--- example/templates/src/articles.html | 2 +- stupid_template_engine.php | 11 ++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/docu/language_definition.html b/docu/language_definition.html index 58d2abd..f4bbf52 100644 --- a/docu/language_definition.html +++ b/docu/language_definition.html @@ -163,23 +163,29 @@ Text elements More info: short if-clause + + \~ + ~ + Text elements + More info: short comparasions + \{ { Text elements - More info: short if-clause + More info: short if-clause, short comparasions \} } Text elements - More info: short if-clause + More info: short if-clause, short comparasions \| | Text elements - More info: short if-clause + More info: short if-clause, short comparasions \\ @@ -277,6 +283,14 @@ ]]> If the variable foo has the content bar, :-) will be returned, :-( otherwise.

+

Short syntax for if-clause

+

Because comparisons are used often, there is an short syntax:

+

~{a|operator|b}

+

This is equavilent to:

+

]]>

+

~, {, | and } can be escaped

+

Because this is implemented as a simple substitution, you can only use Text and Variables. And " must be escaped.

+

WARNING: short comparasions can not be nested! They can be inside short if-clauses, but not the other way around!

ste:not

The ste:not Tag will logically invert its content. If it is an empty text (i.e. false), it will return a non-empty text (i.e. true) and vice versa.

diff --git a/example/templates/src/articles.html b/example/templates/src/articles.html index e007f3e..c0db692 100644 --- a/example/templates/src/articles.html +++ b/example/templates/src/articles.html @@ -24,7 +24,7 @@ There are more than 3 articles? - + ~{$articles_n|gt|3}

There are more articles.

diff --git a/stupid_template_engine.php b/stupid_template_engine.php index d36352d..d88adb4 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -83,8 +83,7 @@ function find_closing_bracket($text, $opening, $closing) function unescape_text($text) { - $text = preg_replace("/(?:(?.*?\\<\\s*\\/\\s*ste:comment\\s*\\>/s", "", $code); /* Remove comments */ + $code = preg_replace( /* Transform short form of comparison (~{a|op|b}) to long form */ + "/(?:(?", + $code + ); $code = preg_replace( /* Transform short form of if-clause (?{cond|then|else}) to long form */ "/(?:(?\$1\$2\$3
", $code ); - /* Unescape \? \{ \} \| */ + /* Unescape \? \~ \{ \} \| */ $code = preg_replace("/(?:(?