diff options
author | Kevin Chabowski <kevin@kch42.de> | 2011-09-23 22:24:16 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2011-09-23 22:24:16 +0200 |
commit | 1619126d7d87538082dd58645a37f528adb1deeb (patch) | |
tree | 33a96610937ca591fa5bcac5aef556b8b2300c02 | |
parent | bc0ea34989618d8747076c322c14d0cd6af591b2 (diff) | |
download | ste-1619126d7d87538082dd58645a37f528adb1deeb.tar.gz ste-1619126d7d87538082dd58645a37f528adb1deeb.tar.bz2 ste-1619126d7d87538082dd58645a37f528adb1deeb.zip |
Added short syntag for comparison.
~{a|op|b}
Also updated the documentation and made unescape_text() a alias of
PHP's stripcslashes().
-rw-r--r-- | docu/language_definition.html | 20 | ||||
-rw-r--r-- | example/templates/src/articles.html | 2 | ||||
-rw-r--r-- | 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 @@ -164,22 +164,28 @@ <td>More info: <a href="#builtin_if_short">short if-clause</a></td> </tr> <tr> + <td>\~</td> + <td>~</td> + <td><a href="#basic_elems_text">Text elements</a></td> + <td>More info: <a href="#builtin_if_short">short comparasions</a></td> + </tr> + <tr> <td>\{</td> <td>{</td> <td><a href="#basic_elems_text">Text elements</a></td> - <td>More info: <a href="#builtin_if_short">short if-clause</a></td> + <td>More info: <a href="#builtin_if_short">short if-clause</a>, <a href="#builtin_if_short">short comparasions</a></td> </tr> <tr> <td>\}</td> <td>}</td> <td><a href="#basic_elems_text">Text elements</a></td> - <td>More info: <a href="#builtin_if_short">short if-clause</a></td> + <td>More info: <a href="#builtin_if_short">short if-clause</a>, <a href="#builtin_if_short">short comparasions</a></td> </tr> <tr> <td>\|</td> <td>|</td> <td><a href="#basic_elems_text">Text elements</a></td> - <td>More info: <a href="#builtin_if_short">short if-clause</a></td> + <td>More info: <a href="#builtin_if_short">short if-clause</a>, <a href="#builtin_if_short">short comparasions</a></td> </tr> <tr> <td>\\</td> @@ -277,6 +283,14 @@ </ste:if>]]></pre></code> If the variable <code>foo</code> has the content <code>bar</code>, <code>:-)</code> will be returned, <code>:-(</code> otherwise. </p> + <h4 id="builtin_cmp_short">Short syntax for if-clause</h4> + <p>Because comparisons are used often, there is an short syntax:</p> + <p><code>~{a|operator|b}</code></p> + <p>This is equavilent to:</p> + <p><code><![CDATA[<ste:cmp text_a="a" op="operator" text_b="b" />]]></code></p> + <p><code>~</code>, <code>{</code>, <code>|</code> and <code>}</code> can be <a href="#escaping">escaped</a></p> + <p>Because this is implemented as a simple substitution, you can only use <a href="#basic_elems_text">Text</a> and <a href="#basic_elems_variable">Variables</a>. And <code>"</code> must be escaped.</p> + <p><strong>WARNING:</strong> short comparasions can not be nested! They can be inside <a href="#builtin_if_short">short if-clauses</a>, but not the other way around!</p> <h3 id="builtin_not">ste:not</h3> <p>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.</p> 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 @@ <ste:comment>There are more than 3 articles?</ste:comment> <ste:set var="articles_n"><ste:arraylen array="articles" /></ste:set> <ste:if> - <ste:cmp var_a="articles_n" op="gt" text_b="3" /> + ~{$articles_n|gt|3} <ste:then> <p>There are <a href="#">more articles</a>.</p> </ste:then> 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", "$", $text); - return str_replace("\\\\", "\\", $text); + return stripcslashes($text); } function tokenize_text($text) @@ -233,13 +232,19 @@ function parse($code) { /* Precompiling... */ $code = preg_replace("/\\<\\s*ste:comment\\s*\\>.*?\\<\\s*\\/\\s*ste:comment\\s*\\>/s", "", $code); /* Remove comments */ + $code = preg_replace( /* Transform short form of comparison (~{a|op|b}) to long form */ + "/(?:(?<!\\\\)~)(?:(?<!\\\\)\\{)(.*?)(?:(?<!\\\\)\\|)(.*?)(?:(?<!\\\\)\\|)(.*?)(?:(?<!\\\\)\\})/s", + "<ste:cmp text_a=\"\$1\" op=\"\$2\" text_b=\"\$3\" />", + $code + ); $code = preg_replace( /* Transform short form of if-clause (?{cond|then|else}) to long form */ "/(?:(?<!\\\\)\\?)(?:(?<!\\\\)\\{)(.*?)(?:(?<!\\\\)\\|)(.*?)(?:(?<!\\\\)\\|)(.*?)(?:(?<!\\\\)\\})/s", "<ste:if>\$1<ste:then>\$2</ste:then><ste:else>\$3</ste:else></ste:if>", $code ); - /* Unescape \? \{ \} \| */ + /* Unescape \? \~ \{ \} \| */ $code = preg_replace("/(?:(?<!\\\\)\\\\\\?)/s", "?", $code); + $code = preg_replace("/(?:(?<!\\\\)\\\\~)/s", "~", $code); $code = preg_replace("/(?:(?<!\\\\)\\\\\\{)/s", "{", $code); $code = preg_replace("/(?:(?<!\\\\)\\\\\\})/s", "}", $code); $code = preg_replace("/(?:(?<!\\\\)\\\\\\|)/s", "|", $code); |