From 8e35fad33de1fbecea7c29776f7afab6a4b88a48 Mon Sep 17 00:00:00 2001
From: Kevin Chabowski
Date: Sun, 15 Jan 2012 13:04:03 +0100
Subject: Added builtin ste:get and fixed a code generation bug.
* ste:get allows us to get a variable's content dynamically.
* (') was escaped for TextNodes, which resulted to:
\'
should have been:
'
---
docu/language_definition.html | 9 +++++++++
example/index.php | 4 ++++
example/templates/src/articles.html | 3 +++
stupid_template_engine.php | 11 +++++++++--
4 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/docu/language_definition.html b/docu/language_definition.html
index 281baea..c4dd98b 100644
--- a/docu/language_definition.html
+++ b/docu/language_definition.html
@@ -63,6 +63,7 @@
ste:load
ste:block
ste:set
+ ste:get
ste:calc
ste:mktag
@@ -435,6 +436,14 @@
<ste:set var="temp"><ste:foo /></ste:set>
<ste:bar baz="$temp" />
+ ste:get
+ ste:get will return the content of a variable. The parameter var
takes the name of the variable to get. Usefult, if you want to get a variable which name is stored in a variable.
+
+ Example:
+<ste:get var="$foo" />
+ This will get the variable with the name that is stored in the variable foo
.
+
+
ste:calc
To perform mathematical calculations, you can use ste:calc. ste:calc calculates the mathematical formula it is wrapped around and returns the result. The formula is in the usual infix-notation [ext. Link] and has these operators: +
, -
, *
, /
and ^
. Numbers are always decimal, the decimal mark is .
and numbers can be prefixed with an -
to indicate a negative number. It is a good idea to wrap a negative number in brackets to prevent wrong evaluation (because -
is also an operator). Calculations can be grouped with brackets: (
and )
.
Real numbers are supported, complex numbers not.
diff --git a/example/index.php b/example/index.php
index 3d2b1ca..14ca576 100644
--- a/example/index.php
+++ b/example/index.php
@@ -52,6 +52,10 @@ $ste->vars["articles"] = array(
array("author" => "baz", "title" => "whatever...", "timestamp" => 1316550000, "excerpt" => "...", "full" => "..........")
);
+$ste->vars["foo"] = "baz";
+$ste->vars["bar"] = "lol";
+$ste->vars["baz"] = array("lol" => "cool");
+
# Execute the template and output the result
echo $ste->exectemplate("articles.html");
diff --git a/example/templates/src/articles.html b/example/templates/src/articles.html
index 108a3b8..0f20aeb 100644
--- a/example/templates/src/articles.html
+++ b/example/templates/src/articles.html
@@ -46,4 +46,7 @@
Bla
+
+ Get a variable's content dynamically
+
diff --git a/stupid_template_engine.php b/stupid_template_engine.php
index 2e11ed4..48164c1 100644
--- a/stupid_template_engine.php
+++ b/stupid_template_engine.php
@@ -44,7 +44,7 @@ class VariableNode extends ASTNode
public $arrayfields;
public function transcompile()
{
- $varaccess = '@$ste->vars[' . (is_numeric($this->name) ? $this->name : '\'' . escape_text($this->name) . '\''). ']';
+ $varaccess = '@$ste->vars[' . (is_numeric($this->name) ? $this->name : '"' . escape_text($this->name) . '"'). ']';
foreach($this->arrayfields as $af)
{
if((count($af) == 1) and ($af[0] instanceof TextNode) and is_numeric($af->text))
@@ -778,6 +778,13 @@ $ste_builtins = array(
return $code;
},
+ "get" => function($ast)
+ {
+ if(empty($ast->params["var"]))
+ throw new ParseCompileError("Transcompile Error: var missing in .", $ast->tpl, $ast->offset);
+
+ return "\$outputstack[\$outputstack_i] .= \$ste->get_var_by_name(" . _transcompile($ast->params["var"], True) . ");";
+ },
"calc" => function($ast)
{
$code = "\$outputstack[] = '';\n\$outputstack_i++;\n";
@@ -790,7 +797,7 @@ $ste_builtins = array(
function escape_text($text)
{
- return addcslashes($text, "\r\n\t\$\0..\x1f\\'\"\x7f..\xff");
+ return addcslashes($text, "\r\n\t\$\0..\x1f\\\"\x7f..\xff");
}
function _transcompile($ast, $no_outputstack = False) /* The real transcompile function, does not add boilerplate code. */
--
cgit v1.2.3-70-g09d2