DELAEMON BLOG

Live as if you were to die tomorrow. Learn as if you were to live forever.

Apache Module ClearSilver

前作ったhello worldモジュールでClearSilverを使って表示してみる

http://delaemon.hatenablog.jp/entry/2013/04/24/010326

Makefile
コマンドをapxs → apxs2へ。ClearSilverのインストールされたパスを指定。読み込むライブラリを指定。

#   the used tools
APXS=apxs2
// ~省略
INCLUDES=-I/usr/include/ClearSilver
LIBS=-L/usr/lib/ -lneo_cs -lneo_utl

mod_hello_world.c
読み込み

#include "ClearSilver.h"

表示の出力のみ行う関数

static NEOERR *render_cb(void *obj, char *s)
{
    request_rec *r = (request_rec *)obj;
    ap_rputs(s, r);
    return STATUS_OK;
}

hdfファイルの読み込み・値のセット、csファイルの読み込み・レンダリング・リソース解放、表示を出力する関数の呼び出し

static void output_handler(request_rec *r)
{
    HDF *hdf;
    CSPARSE *cs;
    NEOERR* err;
    unsigned int seed;
    int num;
    time(&seed);
    srand(seed);
    num = rand() % 4;

    hdf_init(&hdf);
    hdf_set_int_value(hdf, "num", num);
    hdf_set_value(hdf, "name", "RED");
    hdf_read_file(hdf, "/dir/hello_world/test.hdf");

    cs_init(&cs, hdf);
    cs_parse_file(cs, "/dir/hello_world/test.cs");

    cs_render(cs, r, render_cb);
    cs_destroy(&cs);
    hdf_destroy(&hdf);
}

output_handler関数の呼び出し

static int hello_world_handler(request_rec *r)
{
    if (strcmp(r->handler, "hello_world")) {
        return DECLINED;
    }
    r->content_type = "text/html";

    if (!r->header_only)
        output_handler(r);
    return OK;
}

hdfファイル

user {
    name = でらえもん
}
color {
    0 = ゼロ
    1 = 青
    2 = 赤
}
user_list {
    0 {
        name=ほげ
        msg =hoge!!
    }
    1 {
        name=ふが
        msg =fuga!!
    }
    2 {
        name=ふー
        msg =foo!!
    }
}

csファイル

<html>
<head>
<meta charset="utf-8">
<title>hello world!</title>
</head>
<body>

--- IF/ELIF/ELSE ---<br>
<?cs if:name == "BLUE" ?>
IF<?cs var:color.1 ?>
<?cs elif:name == "RED" ?>
ELIF<?cs var:color.2 ?>
<?cs else ?>
ELSE<?cs var:color.0 ?>
<?cs /if ?>
<br>

--- Array ---<br>
var:user.name = <?cs var:user.name ?><br>
var:num = <?cs var:num ?><br>
var:color[num] = <?cs var:color[num] ?><br>

---Loop---<br>
0から3まで1ずつインクリメント<br>
<?cs loop:x = #0, #3, #1 ?><?cs var:x ?>,<?cs /loop ?><br>
1から10まで2ずつインクリメント<br>
<?cs loop:x = #1, #10, #2 ?><?cs var:x ?>,<?cs /loop ?><br>

---Each---<br>
<?cs each:item=user_list?>
key: <?cs name:item ?>, name: <?cs var:item.name ?>, msg: <?cs var:item.msg ?><br>
<?cs /each ?>

</body>
</html>

apache停止・コンパイル・apache起動

$ sudo /etc/init.d/apache2 stop;sudo make clean;sudo make all install; sudo /etc/init.d/apache2 start

http://host/hello_worldにアクセスするとこんな表示
f:id:delaemon:20130502122426p:plain