summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/misc/picotemplate/README.mkdn
blob: 266f4bfa5e9a22087184652384dbeae4fbd5f29c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
PICOTEMPLATE - a tiny template engine for embedded HTML
=======================================================

Picotemplate is a tiny template engine (preprocessor) designed to embed HTML (or other types of document) within the source code of any programming language.

There are often cases where you would like to generate HTML within a program.  In such cases, the easiest way would be to use a DSL (domain-specific language) to embed the HTML in the source code and preprocess it.  Picotemplate is a preprocesser designed for such an use-case.

As of now, Perl, C++, JavaScript, and JSX are the supported languages.

THE LOGIC
---------

* lines starting with "?" are considered as embedded document
* <?= ?> within embedded document are considered as expressions (that return strings)
* the output is accumulated to variable named "_" (or $output in case of perl)
* filenames starting with an underscore (_) will be preprocessed, and the name of the output file will be the same omitting the leading underscore (e.g. _foo.cc will be preproccessed and converted to foo.cc)

EXAMPLE
-------

The following code (in _foo.cc) will be preprocessed and converted to a function (in foo.cc) that returns an function building an HTML snippet.

```
std::string unordered_list(const std::vector<std::string>& strs)
{
    std::string _; // output is accumulated to _
?<ul>
    for (std::vector<std::string>::const_iterator i = strs.begin();
         i != strs.end();
         ++i) {
?<li><?= escapeHTML(*i) ?></li>
    }
?</ul>
    return _;
}
```

To preprocess a source file, simply run the command with the name of the source file to preprocess.  The following exmaple preprocesses _foo.cc_ (template files should start with an underscore) and produces _foo.cc_.

```
$ picotemplate.pl _foo.cc
```