summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2012-01-15 13:04:03 +0100
committerKevin Chabowski <kevin@kch42.de>2012-01-15 13:04:03 +0100
commit8e35fad33de1fbecea7c29776f7afab6a4b88a48 (patch)
tree45140f6f610cc0481e5c6b0bc0eb932578bad972
parent7807fca367008ceaa8ff1b5a1b7f040520ee1cad (diff)
downloadste-8e35fad33de1fbecea7c29776f7afab6a4b88a48.tar.gz
ste-8e35fad33de1fbecea7c29776f7afab6a4b88a48.tar.bz2
ste-8e35fad33de1fbecea7c29776f7afab6a4b88a48.zip
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: '
-rw-r--r--docu/language_definition.html9
-rw-r--r--example/index.php4
-rw-r--r--example/templates/src/articles.html3
-rw-r--r--stupid_template_engine.php11
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 @@
<li><a href="#builtin_load">ste:load</a></li>
<li><a href="#builtin_block">ste:block</a></li>
<li><a href="#builtin_set">ste:set</a></li>
+ <li><a href="#builtin_get">ste:get</a></li>
<li><a href="#builtin_calc">ste:calc</a></li>
<li><a href="#builtin_mktag">ste:mktag</a></li>
</ol>
@@ -435,6 +436,14 @@
<code><pre>&lt;ste:set var="temp"&gt;&lt;ste:foo /&gt;&lt;/ste:set&gt;<br />&lt;ste:bar baz="$temp" /&gt;</pre></code>
</p>
+ <h3 id="builtin_get">ste:get</h3>
+ <p>ste:get will return the content of a variable. The parameter <code>var</code> takes the name of the variable to get. Usefult, if you want to get a variable which name is stored in a variable.</p>
+ <p>
+ Example:<br />
+<code><pre>&lt;ste:get var="$foo" /&gt;</pre></code>
+ This will get the variable with the name that is stored in the variable <code>foo</code>.
+ </p>
+
<h3 id="builtin_calc">ste:calc</h3>
<p>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 <a href="http://en.wikipedia.org/wiki/Infix_notation">infix-notation [ext. Link]</a> and has these operators: <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> and <code>^</code>. Numbers are always decimal, the decimal mark is <code>.</code> and numbers can be prefixed with an <code>-</code> to indicate a negative number. It is a good idea to wrap a negative number in brackets to prevent wrong evaluation (because <code>-</code> is also an operator). Calculations can be grouped with brackets: <code>(</code> and <code>)</code>.</p>
<p>Real numbers are supported, complex numbers not.</p>
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 @@
<ste:repeat n="10">
<p>Bla</p>
</ste:repeat>
+
+ <h3>Get a variable's content dynamically</h3>
+ <ste:get var="${foo}[$bar]" />
</ste:block>
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 <ste:get>.", $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. */