From 737defd22dcc03e633d7323d63e378c017319cdc Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Fri, 9 Sep 2011 16:20:00 +0200 Subject: Language definition finished --- language_definition.html | 490 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 490 insertions(+) create mode 100644 language_definition.html (limited to 'language_definition.html') diff --git a/language_definition.html b/language_definition.html new file mode 100644 index 0000000..6eccfa5 --- /dev/null +++ b/language_definition.html @@ -0,0 +1,490 @@ + + + + Language definition - Stupid Template Engine + + + + +

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. +
    3. Variable
    4. +
    5. Tag
    6. +
    7. + Pseudotag +
        +
      1. ste:comment
      2. +
      3. ste:rawtext
      4. +
      +
    8. +
    +
  2. +
  3. Escaping special chars
  4. +
  5. + Builtin tags +
      +
    1. ste:if
    2. +
    3. ste:cmp
    4. +
    5. ste:not
    6. +
    7. ste:even
    8. +
    9. ste:for
    10. +
    11. ste:foreach
    12. +
    13. ste:infloop
    14. +
    15. ste:break
    16. +
    17. ste:continue
    18. +
    19. ste:load
    20. +
    21. ste:block
    22. +
    23. ste:set
    24. +
    25. ste:calc
    26. +
    27. ste:mktag
    28. +
    +
  6. +
+ +

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 SequenceResultCan be used inNotes
\$$Text elements
\""Tag parameter values
\''Tag parameter values
\??Text elementsMore info: short if-clause
\{{Text elementsMore info: short if-clause
\}}Text elementsMore info: short if-clause
\||Text elementsMore info: short if-clause
\\\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!

+ +

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 valueDescription
eqa and b are equal
neqa and b are not equal
lta is less than b
ltea is less or equal b
gta is greater than b
gtea 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. +

+ +

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 nameMandatory?Description
startYesBegin counting at this number.
stopYesStop counting at this number (inclusive).
stepNoWhat amount should be added to the counter at each round? (Default: 1)
counterNoThe 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 nameMandatory?Description
arrayYesThe array to be looped through.
keyNoThe array key will be stored in this variable (expects variable name).
valueYesThe value of the current element will be stored in this variable (expects variable name).
counterNoExpects 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. +

+

Code in blocks will be executed at last.

+ +

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

+ +

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
]]>
+

+ + -- cgit v1.2.3-70-g09d2