Placeholders

When the PTP engine is given a template, it looks through that template for placeholders. Placeholders are generally denoted by curly braces - { }. This is the standard setup, although the characters used to open and close the placeholder can be changed by the PHP programmer if required. Everything outside the placeholders is totally ignored, and everything inside the placeholders is compiled, if possible (Nested placeholders cannot be compiled, and should therefore only be used when absolutely necessary).

For example:

This is some text. It will be ignored by the parser, and displayed exactly as it is.
 
{ 'This is a placeholder. It will be parsed, and appropriate action taken.' }
 
This is some more text.
 

Of course, placeholders should contain PTPScript code. In the example above, the text inside the placeholder is contained in a string, so it is valid code. Because nothing has been done to it, it will be displayed as it is. The same is true of Variables and Expressions, except for assignment, which produces no output. The text outside the placeholder can be anything - HTML, CSS, SQL, C++ - it doesn't matter, so long as it doesn't contain curly braces.

If the text does contain curly braces, the Show command should be used.

Here's another, more likely example:

<h1>This is a PTPScript example.</h1>
 
{ if 2 < 3 }
 
<p>2 is less than 3.</p>
 
{ else }
 
<p>Something is wrong - 2 is not less than 3!</p>
 
{ endif }
 
<p>Well, that's the end of our example!</p>

The code above should be easy enough to work out. The above example will output a fair amount of whitespace, so a way to reduce it might be:

<h1>This is a PTPScript example.</h1>
 
{   if 2 < 3
 
}<p>2 is less than 3.</p>{
 
    else
 
}<p>Something is wrong - 2 is not less than 3!</p>{
 
    endif
 
}
 
<p>Well, that's the end of our example!</p>

This will reduce the amount of whitespace displayed, because everything outside the placeholders is displayed as-is, whereas everything inside disappears as part of the placeholder expression (and may or may not result output - commands do not display any output).

However, the above example is not as easy to read as it could be. An alternate is to use the whitespace suppression symbols to suppress newlines and/or whitespace on either side of placeholders, as required. These symbols are ellipses (…) by default, but the backend programmer may choose to change them. The example code would now be written as:

<h1>This is a PTPScript example.</h1>
 
…{   if 2 < 3 }…
 
<p>2 is less than 3.</p>
 
…{   else }…
 
<p>Something is wrong - 2 is not less than 3!</p>
 
…{   endif }…
 
<p>Well, that's the end of our example!</p>

The above example would rely upon the GreedyLinebreakSuppression Directive being used, otherwise only one linebreak would be suppressed by each ellipsis. The default Directives are LinebreakSuppression and NoWhitespaceSuppression, and examples elsewhere will be written accordingly.

Each placeholder contains a single instruction.

Whitespace suppresion symbols need to appear immediately before or after a placeholder, if they are to be used.

Nested placeholders

Placeholders can contain placeholders, but this is discouraged for performance reasons, unless absolutely necessary. The reason for this is that any placeholder containing another placeholder cannot be compiled (because the contents are uncertain) and therefore have to be parsed at runtime. Interpretation is always slower than running pre-compiled code, so there will be a performance hit.

Here is an example to demonstrate:

{ Foo{SomeNumber}Bar }

In the example above, two Variables are shown. The second one is the one within the inner placeholders - SomeNumber. The first is contained in the outer placeholder. Its name is formed from Foo, plus the result of SomeNumber (inner placeholders are evaluated first, just like brackets), and then Bar. So, if SomeNumber had a value of 15, the resulting variable name would be Foo15Bar.

A more complicated example would be if SomeNumber contained the string " = 4 * ". This would then make the outer placeholder into the expression Foo = 4 * Bar. Although very useful in some situations, there is no way for the PTP engine to ever know what expression would result, and so it cannot be compiled.

ptpscript/basic_syntax/placeholders.txt · Last modified: 2007/01/24 10:15
 
 
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki