I have just putted this piece of code into the
repository together with small wiki article.
Template techniques are widely used in programming languages and, of course, the web. This small piece of code that I have "cleverly" called Lambda Templates is yet another string substitution tool (as I have found out later initially very similar to String Templates by Terrence Parr).
Usually, templates mechanism tries to follow procedural paradigm of text generation by simple replacing of a bunch of specially marked placeholders with passed values. For my needs, I wanted to have something very short at hand: to call a method and process the result further within the program code.
To set a constant string value to some variable, one calls $p->setVar('foo','bar');. Apparently, $p->getVar('foo'); will return the corresponding value.
Now the name Lambda featuring the title, comes from Lambda calculus by Alonzo Church, where one can observe a sort of substitution mechanism for resolution of specially build logical expression. Well, actually the resemblance ends at the point of using "/" that mimics lambda symbol in lambda expressions.
Once again, the idea behind a lambda template is actually as trivial as this: /fruit. this is a fruit - an example of parametrized template's value. Because it's inspired by lambda calculus, we don't give a name for this expression inside the value. The comma separated list of argument names between LAMBDA and DOT symbols, are actually names that will be reserved for substitution after DOT.
Grammar will be something like
expression := body | LAMBDA (argument COMMA)* DOT body;
body := CHAR*;
Further examples are:
$p = new LambdaTemplates();
$p->setVar('x','10');
$p->setVar('what is x','/x.is /x = x?');
echo $p->processTemplate('question: %what is x%');
$p = new LambdaTemplates();
$p->setVar('titel','herr');
$p->setVar('name','y.y.');
$p->setVar('titelname','/titel,name.titel name');
echo $p->processTemplate('question: %titelname%');
Current implementation does not support a proper functional paradigm for templating with closure and application constructions (and maybe proper reduction). All parametrized names inside argument list are applied globally, that is extracted from the global scope of values and substituted to the expression.
No comments:
Post a Comment