User:Krauss/arXiv-1/Simplest examples
Complete technical description of the simplest exemples of template systems.
Document generation
[edit]For reference to concrete typical applications we can remember form letters and letter frames.
- Form letter: a letter written from a template, frequently used for "spam", by marketing campaigns. It allows a mass production of very similar letters -- ex. changing destinatary name, "Dear mr. John", "Dear ms. Mary".
- Letter frame: any content is filled in the blank of a "header and footer" frame template. It allows a mass production of non-similar content letters, with a "standard frame". Both are examples of (production of) a set of "template generated documents".
C simplest template system
[edit]This set of files satisfy the "black box criteria". There are 4 files representing the system,
- The C source code file: represent the template.
- The "bash file": represent the content.
- The output files: out1.htm and out2.htm represent the output documents.
- "data model": a programmer restriction that "all output are simple printf or printf with argv parameters", and "all logic depend only of the argv parameter".
With this mapping and model considerations, many other complex C program can view as template. This example implements a "Letter form" or "Mail merge" simple applications.
FORM LETTER TEMPLATE (simplestTS.c):
#include <stdio.h>
void main(int argc, char** argv) {
printf ("<html>Hello %s!</html>", argv[1]);
}
CONTENTS (simplestTS.sh):
./simplestTS.exe John > out1.htm
./simplestTS.exe Mary > out2.htm
OUTPUT-1 (out1.htm):
<html>Hello Jonh!</html>
OUTPUT-2 (out2.htm):
<html>Hello Mary!</html>
Perl simplest template engine
[edit]This set of files satisfy the "black box criteria" and the "strict syntax criteria" (for a simple replace script template language).
This Perl simplest template system do the some thing that the C simplest. It is, perhaps, the tiniest software for template engine. There are 4 files representing the system,
- The Perl processing with the Perl template interpreter: represent the template engine and its source code.
- The template: a "strict" template file (!).
- The "bash file": represent the content.
- The output files: out1.htm and out2.htm represent the output documents.
- "data model": it is expressed (enbodyed) by the template engine.
The sintax of the templates are very simple, there are only document output source code and simple place holders, "_ARG1_", "_ARG2_", etc.
TEMPLATE ENGINE (simplestTS.pl):
$template = join('',<STDIN>);
$template =~ s/_ARG([0-9]+)_/$ARGV[$1]/gs;
print $template;
FRAME LETTER TEMPLATE (simplestTS_frame.tpl):
<html><center>MY COMPANY</center><hr/> _ARG1_ <hr/></html>
FORM LETTER TEMPLATE (simplestTS_letter.tpl):
<html>Hello _ARG1_!</html>
Running the template engine with the simplestTS_letter.tpl template file, we obtain the letters,
%perl simplestTS.pl John < simplestTS_letter.tpl > out1.htm %perl simplestTS.pl Mary < simplestTS_letter.tpl > out1.htm
We obtain the same out1.htm and out2.htm files, as the "C simplest". The main difference is that the "template script" is processed by a specialized engine (the Perl software), not by the "Perl interpretor language" (see C and PHP simplest cases).
Running the template engine with the simplestTS_frame.tpl template file, we obtain the letters with frames,
%perl simplestTS.pl ...ALL_THE_CONTENT... < simplestTS_frame.tpl > out.htm
OUTPUT (out.htm):
<html><center>MY COMPANY</center><hr/>
...ALL_THE_CONTENT...
<hr/></html>
Dynamic web page generation
[edit]Dynamic web page applications, for reference to concrete ones.
PHP simplest template system
[edit]This set of files satisfy the "black box criteria" and the "strict syntax criteria" (for a unrestrited script template language).
There are 4 files representing the system,
- The PHP source code file: represent the template.
- The "URLs": represent the content.
- The output files at each URL.
- "data model": a programmer restriction that "all not static output are a GET parameter", and "all logic depend only of the GET parameter".
Many other (also complex) PHP program can viewed as template, with this mapping and model considerations.
TEMPLATE (simplestTS.php):
<html>Hello <?= $_GET[arg]?>!</html>
CONTENTS (URLs):
simplestTS.php?arg=John simplestTS.php?arg=May
these URLs at the browser produce the same HTML source code as the "C simplest example".
Template-author interfaces: translating template scripts
[edit]... script parsers... from a standard (for designers) language to a specific template language. ... The translator with the processor is also a processor, in the black-box model.
PLACEHOLDER TEMPLATE, authored by designer:
<html>
<center>TITLE: _ARG1_</center>
<hr/>
.....
_ARG2_
.....
<hr/>
</html>
A simple C template transducer
[edit]... using K-array to
my $template = join('',<STDIN>); #all lines
$template =~ s/([%"\n])/\\$1/gs; # scapes
# list of args in the correct order:
$template =~ s/_ARG([0-9]+)_/@C[] = $1;"%";/ges;
if ($#C>0) {
print "\nprintf (\"$template\",";
print 'argv['.join('], argv[',@C)."]);";
}
} else print "\nprintf \"\\$template\";}";
TEMPLATE from programmer: same as designer, is a simple placeholder template.
TEMPLATE translated to C
printf ("\n<html>\n <center>TITLE: %s</center>\n <hr/> \n %s\n .....\n <hr/>\n</html>",argv[1],argv[2]);
It transformed a "document template" into a "print template".
running the template at a template engine1 (a shell script),
%gcc tpl1.c %./a.out inputA1 inputA2 > outA.htm %./a.out inputB1 inputB2 > outB.htm
A simple C unrestritect template script
[edit]COMPLEX TEMPLATE, the designer's original template reviwed by programmer (adding a include comand and a if block to tpl.c)
<html>
##include(anotherThing.c);
#if (ARG1>'') {
<center>TITLE: _ARG1_</center>
#}
<hr/>
_ARG2_
.....
<hr/>
</html>
Converter (convT2C.pl perl program converts T into a C program) that generates C-templates (like the C simplest exemple) from a unrestrited template as input:
while(my $line = <STDIN>) {
chomp($line);
$line =~ s/%/\\%/gs; # scape C template ctrl
my $args = '';
while ($line =~ s/_ARG([0-9]+)_/%s/s) {
$args .= ", argv[$1]"; # produce the list of args of this line
}
if ($line =~ s/^#//) { # it is a script line
print "\n$line";
} elsif ($args) { # line with args
print "\nprintf (\"\\n$line\"$args);";
} else { # a line without args
print "\nprintf \"\\n$line\";";
}
}
TEMPLATE translated to C
printf "\n<html>";
#include(anotherThing.c);
if (ARG1>'') {
printf ("\n <center>TITLE: %s</center>", argv[1]);
}
printf "\n <hr/> ";
printf ("\n %s ", argv[2]);
printf "\n .....";
printf "\n <hr/>";
printf "\n</html>";
running the template at a template engine2 (a shell script),
%gcc tpl.c %./a.out inputA1 inputA2 > outA.htm %./a.out inputB1 inputB2 > outB.htm