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:calc
    14. 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:

Foo $baz[herpdederp]]]>
Will result in one text-element, but
$baz[herpdederp]]]>
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 vou 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 othe 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 ist 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 (barherpdederp]]>) or can be self-closing (]]>). And they can have additional parameters (or "attributes", using the XML terminology): ]]>

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.: ]]>).

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: bar]]>

Parameters of a tag consists of a name and the corresponding value (wrapped in " or ') seperated by an = .
Parameters are seperated 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 paramter 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: de ]]>

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 comparasions
\{ { Text elements More info: short if-clause, short comparasions
\} } Text elements More info: short if-clause, short comparasions
\| | Text elements More info: short if-clause, short comparasions
\\ \ 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:


	$foo
	Bar
	Baz
]]>
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 equavilent to:

conditionthenelse]]>

?, {, | 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 wth 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 comparasion was true, a non-empty text will be returned, otherwise an empty text, so you can use ste:cmp with ste:if

Example:


	
	:-)
	:-(
]]>
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.

Example:


	$foo
	:-)
	:-(
]]>
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:


	$i
]]>
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:


	Number: $i
Key: $k
Value: $v

]]>
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 othe loop constructs like while loops.

Example:


	
		
		
	
	...
]]>
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 anoter 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:


	bla]]>
master.tpl

]]>

But you can do this:

slave.tpl:

$bar]]>
master.tpl

	
]]>

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

Content:

	Default content

]]>
slave.tpl:


	Much cooler content :-)
]]>
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:

bar]]>
This will set the variable foo to bar.

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


]]>

ste:calc

To perform mathematical calculations, you can use ste:math. ste:math 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:
(2+3+4) * (1.5 - (-0.5))]]> 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 seperated 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:


	
		
	


	 * 2


	$i
]]>
Will output:

8
6
4
2
0
]]>

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 ist the tag's content.

Example:

Foo & bar...]]>
Result:
Foo & 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. substract 1) a variable (variable name given by parameter var).

ste:date

Formats a time using PHPs date 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:

d. M. Y, H:i:s]]>
Result: