ClearSilverを使ってみる


http://www.clearsilver.net/

Cのテンプレートエンジン。


Trac が使ってるので有名(Pythonバインディング)。

Google Groups でも使ってるとかいないとか。


というわけで Hello World してみる(Cで)。

どこにも参考になるところがなかったのでソース読みつつ。


インストールは ./configure && make && make install でOK


hello.c

#include <stdio.h>
#include "ClearSilver.h"

NEOERR *render_cb(void *data, char *s)
{
    printf("%s", s);
    return STATUS_OK;
}

int main()
{
    HDF *hdf;
    CSPARSE *cs;
    hdf_init(&hdf);
    hdf_set_value(hdf, "message", "hello world!");
    cs_init(&cs, hdf);
    cs_parse_file(cs, "hello.cs");
    cs_render(cs, NULL, render_cb);
    cs_destroy(&cs);
    hdf_destroy(&hdf);
    return 0;
}

hello.cs

<?cs var:message ?>


でこれを

$ gcc -o hello hello.c -I/usr/local/include/ClearSilver -lneo_cs -lneo_cgi -lneo_utl -lz
$ ./hello
hello world!


Cのテンプレートエンジンは少ない(知らないだけかも)ので重宝しそう。

Perl なら Template-Toolkit やら HTML::Template やら使いやすいやつが山ほどあるんだけどなぁ。