Recommend this page to a friend! |
Classes of wim niemans | SIREN PHP Templating Library | recursion.md | Download |
|
DownloadHowto use recursion in SIREN using simple PHPBasic requirements are a text containing (named) markers and an array that maps these markers to replacement text. The text itself has a name too and is also contained in the array. What SIREN does is scan the text, detect the marker, extract its name and replace it with the corresponding text found in the array. In SIREN the (named) markers are called variables and they have a (mapped) value (replacement text).
Let's adjust the example with a more sophisticated example: define a variable 'fullName' that has embedded the individual name parts. Punctuation could be added.
So far so good. We've embedded some variables in the value of the variable 'fullName'. And in simple cases like this one, and without recursion we can and must parse the text twice. But this is dropping a dependency into your code. A dependency on the structure of the main variable 'fullName' and the embedded variables with their respective values. However with recursion, this example would reduce to
And this makes sense since we rather would define
to have an output like 'My name is King, Martin Luther'. This shows that SIREN replaces the variable 'fullName' with text that embeds a few other variables, which is detected by SIREN, and results in replacing those embedded variables with their respective values. All in one go.
In above example we need to parse the text three times. This is the case where recursion comes in very handy,and this example would reduce to echo $snippet->parse('output','text'); // I am greeting you on behalf of Martin Luther King because SIREN detects that the replaced text of a variable contains embedded variables and parses them automatically, detects that ...... etc. # 1. So, in example 2 'fullName' is replaced by '{firstName} {middleName} {lastName}' is replaced by their final values # 2. And, in example 3, 'someHuman' is replaced by '{fullName}', is replaced again by, using recursion, see (1) Real benefit is found when you abstract your templates with symbolic variables. Like the date needs different formats depending on the locale, the name must be written differently depending on the audience. Americans are used to 'Martin Luther King' or 'Martin L. King', and europeans merely use 'King, Martin Luther' or 'King, M.L.'.
Than, there is also a difference in usage of a fullName, a commonName and a officialName.
In above examples, one could use the variables 'fullName', 'commonName' and 'officialName' at will in the templates, getting every time a correct spelling. It's a matter of assigning the right variables, and the design of the result is separated from your coding. And there is more. Suppose the legal department does not allow the free usage and wants the choice of name-format should be in the code.
# 3. So, in example 6, the variable {typeOf}Name is first replaced by 'fullName', 'commonName' or 'officialName' and the resulting variable is recursive replaced. In SIREN the depth of the recursion can be set:
For more fine examples see demo.php. |