The STE Template/Programming Language

This is the documentation of the Template/Programming Language of the Stupid Template Engine, or STE T/PL

TOC

  1. Basic elements
    1. Text
    2. Variable
    3. Tag
    4. Pseudotag
      1. ste:comment
      2. ste:rawtext
  2. Escaping special chars
  3. Builtin tags
    1. ste:if
    2. ste:cmp
    3. ste:not
    4. ste:even
    5. ste:for
    6. ste:foreach
    7. ste:infloop
    8. ste:break
    9. ste:continue
    10. ste:load
    11. ste:block
    12. ste:set
    13. ste:get
    14. ste:calc
    15. ste:mktag
  4. Standard Library
    1. ste:escape
    2. ste:strlen
    3. ste:arraylen
    4. ste:inc
    5. ste:dec
    6. ste:date

Basic Elements

The STE T/PL consists of four basic elements:

Text

Everything that is not a Variable, a Tag or a Pseudotag is Text.

Also everything that is wrapped in the ste:rawtext pseudotag will be Text, whereby all Tags and Variables will not be parsed.

Example:

<ste:rawtext>Foo <ste:bar>$baz[herpdederp]</ste:baz></ste:rawtext>
Will result in one text-element, but
Foo <ste:bar>$baz[herpdederp]</ste:baz>
will result in one text-element and one tag-element containing one variable-element.

Variable

Variables start with a $ or can be wrapped within ${ and }, so you can write this: ${foo}ish

Variable have names, these names can consist of letters (english alphabet; upper and lower case), numbers and underscores (_). As a regex: [a-zA-Z0-9_]+

A variable can also be an array. To access an array, wrap the desired fieldname within [ and ]. A fieldname can be constructed of Text and other Variables. So you can dynamically access fields: $foo[$bar]. These fieldnames can also be nested or concatenated: $foo[$bar[baz]][herp][$de[derp]]

If you want a literal $ char, you can escape it: \$

Variables in STE T/PL are typeless, everything is text. In a boolean context, empty text usually represents false, else true.

Tag

A Tag can be compared to a function. A Tag can have parameters and children elements.

A Tag looks like a XML-Tag with the ste-Namespace. Just like their XML counterparts, they can wrap other elements (<ste:foo>bar<ste:baz>herpdederp</ste:baz></ste:foo>) or can be self-closing (<ste:foo />). And they can have additional parameters (or "attributes", using the XML terminology): <ste:foo bar="baz" />

A Tag is wrapped in < and >. The tag's name always start with ste: and can then consist of letters, numbers and underscores (Regex: [a-zA-Z0-9_]+).

If the tag is self-closing, the last char is a / (e.g.: <ste:foo />).

If the tag is a closing one, the first char is a /. An opening Tag does not have a /.An example of an opening-closing Tag pair wrapping the text bar: <ste:foo>bar</ste:foo>

Parameters of a tag consists of a name and the corresponding value (wrapped in " or ') separated by an = .
Parameters are separated by any whitespace (space, tab or newline) char.
Parameter values can consist of Text and Variable elements but not of Tags!
If you need a literal " or ' in a parameter value, you can escape them: \" or \' .
When using variables in parameter values, they will be "replaced" by their value. Because many tags need the variable and not its content, they expect only the variable's name. If you then write foo="$bar", the tag will not operate on the bar variable but on the Variable with the name stored in $bar! So read the instructions to the tag carefully!

Example: <ste:foo bar="baz" herp="literal quote sign: \"">de <ste:derp hehe="hoho$wtf[xd]" /></ste:foo>

Pseudotag

Pseudotags look like normal tags, but they perform special tasks. There are currently two pseudotags:

ste:comment

With the ste:comment pseudotag you can comment your template/code. Everything between <ste:comment> and </ste:comment> will be ignored, before the real tokenization of the code starts.

ste:rawtext

The ste:rawtext pseudotag will output a Text element with its wrapped content. It will prevent parsing of the content. Useful if you are embedding another script language, which uses the $ char or also has a XML-Like syntax. No escape sequences will be translated! Can not be used in Tag parameter values!

Escaping special chars

To get a literal $, " or other special chars, STE T/PL gives you the following escape sequences:

Escape Sequence Result Can be used in Notes
\$ $ Text elements
\" " Tag parameter values
\' ' Tag parameter values
\? ? Text elements More info: short if-clause
\~ ~ Text elements More info: short comparisons
\{ { Text elements More info: short if-clause, short comparisons
\} } Text elements More info: short if-clause, short comparisons
\| | Text elements More info: short if-clause, short comparisons
\\ \ Everywhere, where the other escape sequences are valid

Escape sequences are not translated in Pseudotags

Builtin Tags

STE has some builtin Tags, that makes programming in STE T/PL possible.

ste:if

The ste:if Tag provides an if-clause to STE T/PL.

ste:if can have the subtags ste:then and ste:else. Everything between <ste:if> and </ste:if>, that is not an ste:then or ste:else tag, will be used as the condition.

Because STE T/PL is typeless, every empty string (trailing whitespaces will be ignored) is considered as false, everything else is true.

If the condition is true (i.e. not empty), the content of the ste:then block will be executed. Otherwise the ste:else tag (if present) will be executed.

The ste:then Tag is mandatory, the ste:else tag is optional.

Example:

<ste:if>
$foo
<ste:then>Bar</ste:then>
<ste:else>Baz</ste:else>
</ste:if>
If $foo is not empty, then Bar will be executed, otherwise Baz.

Short syntax for if-clause

Because if-clauses are used often, there is an short syntax:

?{condition|then|else}

This is equivalent to:

<ste:if>condition<ste:then>then</ste:then><ste:else>else</ste:else></ste:if>

?, {, | and } can be escaped

In this variant, the else part is not optional!

WARNING: short if-clauses can not be nested!

ste:cmp

With the ste:cmp tag you can compare two values.

ste:cmp is selfclosing, the compared values are passed by parameters.

It compares two values, a and b using an operator.

a and b can be passed in two ways:

(where x is either a or b).

The operator is passed with the op parameter. Following parameters are available:

op value Description
eq a and b are equal
neq a and b are not equal
lt a is less than b
lte a is less or equal b
gt a is greater than b
gte a is greater or equal b

If the comparisons was true, a non-empty text will be returned, otherwise an empty text, so you can use ste:cmp with ste:if

Example:

<ste:if>
<ste:cmp var_a="foo" op="eq" text_b="bar" />
<ste:then>:-)</ste:then>
<ste:else>:-(</ste:else>
</ste:if>
If the variable foo has the content bar, :-) will be returned, :-( otherwise.

Short syntax for comparisons

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

~{a|operator|b}

This is equivalent to:

<ste:cmp text_a="a" op="operator" text_b="b" />

~, {, | 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 comparisons 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.

Example:

<ste:if>
<ste:not>$foo</ste:not>
<ste:then>:-)</ste:then>
<ste:else>:-(</ste:else>
</ste:if>
If the variable foo is empty (i.e. false), :-) will be returned, :-( otherwise.

ste:even

If the enclosed text is a number, and the number is even, this tag will return a non-empty text (i.e. true), an empty text (i.e. false) otherwise.

ste:for

ste:for provides a counter loop.

ste:for has these parameters:

parameter name Mandatory? Description
start Yes Begin counting at this number.
stop Yes Stop counting at this number (inclusive).
step No What amount should be added to the counter at each round? (Default: 1)
counter No The current number will be stored in this variable (expects a variable name).

Example:

<ste:for start="10" stop="0" step="-1" counter="i">
$i<br />
</ste:for>
Will count from 10 down to 0 and output the number followed by an HTML line break.

ste:foreach

ste:foreach will loop through an array.

ste:foreach has these parameters:

parameter name Mandatory? Description
array Yes The array to be looped through.
key No The array key will be stored in this variable (expects variable name).
value Yes The value of the current element will be stored in this variable (expects variable name).
counter No Expects a variable name. If given, the current number of iterations will be stored in this variable. Starts with 0.

Example:

<ste:foreach array="foo" key="k" value="v" counter="i">
Number: $i<br />
Key: $k<br />
Value: $v<br />
<br />
</ste:foreach>
This code will loop through the array foo and return the counter $i, the key $k and the value $v of the current iteration.

ste:infloop

Create an infinitive loop. You can get out of the loop using the ste:break tag. Can be used to emulate other loop constructs like while loops.

Example:

<ste:infloop>
<ste:if>
<ste:foo />
<ste:then><ste:break /></ste:then>
</ste:if>
...
<ste:infloop>
This code will return ... while <ste:foo /> returns an empty text (i.e. false).

ste:break

When this self-closing tag is called, the current loop (ste:for, ste:foreach, ste:infloop) will be aborted.

ste:continue

When this self-closing tag is called, the current loop(ste:for, ste:foreach, ste:infloop) will go to the next iteration, aborting the current iteration.

ste:load

This self-closing tag loads and executes another template. The name parameter (mandatory) defines the template to load.

Because each template must be parseable and transcompilable by itself, this is not a inclusion of another template. So you can not do this:

slave.tpl:

<ste:foo>
bla
master.tpl
<ste:load name="slave.tpl" />
</ste:foo>

But you can do this:

slave.tpl:

<ste:foo>$bar</ste:foo>
master.tpl
<ste:baz>
<ste:load name="slave.tpl" />
</ste:baz>

ste:block

ste:block provides an easy way for writing master templates. Every block has a name. When a block is defined twice, the second will overwrite the first one.

The name can be set with the name parameter.

Example:
master.tpl

<h1>Content:</h1>
<ste:block name="content">
Default content
</ste:block>
<div class="sidebar">
<ste:block name="sidebar">
Default sidebar
</ste:block>
</div>
slave.tpl:
<ste:load name="master.tpl" />
<ste:block name="content">
Much cooler content :-)
</ste:block>
When executing slave.tpl, master.tpl will be loaded and its content block will be replaced with the new one (Much cooler content :-)) but leave the original sidebar block.

Blocks can not be nested.

ste:set

ste:set will set a variable. The parameter var takes the name of the variable to set. The content of the Tag will be the new content of the variable.

Example:

<ste:set var="foo">bar</ste:set>
This will set the variable foo to bar.

Tag parameter values can not contain Tags. ste:set can be used to bypass this:

<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.

Formulas are evaluated at runtime, not during transcompilation.

Example:
<ste:calc>(2+3+4) * (1.5 - (-0.5))</ste:calc> will return 18.

This Tag is pretty slow, because the formula is not transcompiled and only evaluated at runtime. For some simple increment and decrement operations it is better to use the ste:inc and ste:dec Tags from the standard library.

ste:mktag

ste:mktag allows you to define own Tags using STE T/PL.

The parameter name (mandatory) expects the name of the new tag. If your tag requires some parameters, you can specify them using the optional mandatory parameter. Names of the mandatory parameters are separated by |.

The Variable _tag_parameters (associative array) will hold all given parameters and their values.

With the ste:tagcontent tag you can execute the tags content.

ste:mktag will be transcompiled like any other code. So your custom tag will be almost as fast as a plugin coded in PHP.

Example:

<ste:mktag name="countdown" mandatory="from|counter">
<ste:for start="$_tag_parameters[from]" stop="0" step="-1" counter="$_tag_parameters[counter]">
<ste:tagcontent />
</ste:for>
</ste:mktag>
<ste:mktag name="double">
<ste:calc><ste:tagcontent /> * 2</ste:calc>
</ste:mktag>
<ste:countdown from="5" counter="i">
<ste:double>$i</ste:double><br />
</ste:countdown>
Will output:
10<br/>
8<br />
6<br />
4<br />
2<br />
0<br />

Standard Library

The Standard Library contains some useful tags, which are not builtin but still always available.

ste:escape

Escapes characters that are reserved for HTML (e.g. <, >, ", &). The text to escape is the tag's content.

Example:

<ste:escape>Foo & bar...</ste:escape>
Result:
Foo &amp; bar...

ste:strlen

Returns the length of then content.

ste:arraylen

Returns the number of elements in the array (variable name given by parameter array).

ste:inc

Increments (i.e. add 1) a variable (variable name given by parameter var).

ste:dec

Decrements (i.e. subtract 1) a variable (variable name given by parameter var).

ste:date

Formats a time using PHPs strftime format [ext. Link]. The format is given in the tag's content. You can specify a time (unix timestamp) using the timestamp parameter (defaults to the current time).

Example:

<ste:date timestamp="1316357360">%d. %h. %Y, %H:%M:%S</ste:date>
Result:
18. Sep. 2011, 16:49:20