Recommend this page to a friend! |
Classes of wim niemans | SIREN PHP Templating Library | siren.usage.md | Download |
|
DownloadIntroductionA lot of template engines just replace markers in a text by other text. The concept of boilerplate is their base feature. SIREN adds recursion to these markers, and inherently allows for (recursive) markers in the boilerplate text. In SIREN, markers are called variables, and they have a value that will replace the markers. So, intuitively a variable stands for its value in template texts. Not just for TemplatesThe Snippet class, being the workhorse of SIREN, has no bias for HTML, XML, or whatever. Its engine lacks any sophisticated feature and focusses on strings only. This makes Snippet an ideal utility to generate structured texts. Like texts that must adhere some structure, say some syntax. A beautyfull example on SQL statements can be found in the directory examples. BenefitsBenefits of SIREN include:
Template Variable Naming ConventionsTemplate variables follow a flexible naming convention. A variable name may consist of letters, digits, one or more underscore, period, dash or square brackets. A variable name must be delimited by curly brackets:
If a variable name is invalid, aka does not match the convention above, the complete marker is left as is. However, and now recursion comes into action, a variable name can be 'nested':
So far so good: using recursive variables results in new variable names, which will have a value too. What if that (new) value contains a valid variable name, that is replaced and forms a new variable name that is nested again? Well, intuitively recursion repeats itself on that newly formed variable:
What this means, is that the recursive replacement task processes the complete text until all variables are detected and replaced. And SIREN does that in one go. Invalid variable names, and not recognised variables as well, are left as is. After processing one can tell Snippet to keep those variables in the resulting text, have them removed, or to comment them out as HTML comments. Have a look at the demo.php file in the root directory. Not naming, but an extended feature!A variable name can start with a dollar sign or a question mark. This changes the meaning of a variable:
Usage
Advanced UsageSnippet contains a mechanism to throttle the recursion depth. The reason is that often variables contain values from the outside, i.e. data out of control by the designer, which may or may not contain the marker delimiters '{' or '}'. Recursion can be limited to a 'match' on variable name (basic), avoiding recursion in the value of recognised variables. For debugging purposes there is the setting 'raw' and 'none'. Default is smart recursion, triggerd by 'auto'. Unlimited recursion is restored by the setting 'deep'. To escape recursive parsing of the value, regardless of recursionDepth, one can set the value of a variable as non-parsable.
Finally, some general methods are available:
Tip 'n TricksThe setVar method has three forms:
The advanced form causes the definition for every key with its respective value. The very advanced form results in variables that contain references using square brackets, a bit like you would code them in php. Please note tha square brackets are valid characters in the variable name.
Please note the absence of quotes in the variable names in above example. The getVar call has two forms:
This array is definitely a simple array with numeric keys. Gotcha's
|