ClearSilverのPHPバインディング
tracをセットアップする際、テンプレートエンジンであるClearSilverが必要になったついでにウロウロしていたところ、PHPバインディングがあるとのことなので、さらっと使ってみました。
ただ、最終更新が2004/11/02、バージョン0.1なので、あまり実用的ではないかも知れません。とはいえ、折角なのでSara晒しておきます。
インストールは特に難しい部分はありません。
$ php -v PHP 5.2.5 (cli) (built: Jan 11 2008 18:39:26) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies with Xdebug v2.0.2-dev, Copyright (c) 2002-2007, by Derick Rethans with eAccelerator v0.9.5.2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator $ wget http://www.clearsilver.net/downloads/clearsilver-0.10.5.tar.gz $ tar zxf clearsilver-0.10.5.tar.gz $ cd clearsilver-0.10.5/ $ ./configure $ make $ sudo make install $ cd ../ $ wget http://www.geodata.soton.ac.uk/software/php_clearsilver/php-clearsilver-0.1.tar.gz $ tar zxf php-clearsilver-0.1.tar.gz $ cd php-clearsilver-0.1/ $ ./configure --with-clearsilver=/usr/local/ $ make $ sudo make install $ sudo echo "extension=clearsilver.so" >> /path/to/php.ini $ sudo /path/to/apachectl graceful $
サンプルコードですが、ClearSilver PHP Extensionにあるものを若干修正して試しました。
<?php /** * @see http://www.geodata.soton.ac.uk/software/php_clearsilver/index.html */ // initialise the Hierarchical Data Format (HDF) $hdf = hdf_init(); // populate HDF hdf_set_value($hdf, 'person.name.fore', '日本語'); hdf_set_value($hdf, 'person.name.middle', '表領域'); hdf_set_value($hdf, 'person.name.nick', 'ソフト'); hdf_set_value($hdf, 'person.name.sur', 'まだ残って高'); hdf_set_value($hdf, 'person.profession', '①㈱'); hdf_set_value($hdf, 'person.age', 71); hdf_set_value($hdf, 'person.alive', false); // initialise a Clearsilver parse tree (CS) with the HDF $cs = cs_init($hdf); // create template (from a string in this case) $template = '<p> <b><?cs var person.name.fore ?> "<i><?cs var person.name.nick ?></i>" <?cs var person.name.sur ?></b>, the greatest <?cs var person.profession ?> of them all, <?cs if:person.alive ?> is <?cs var person.age ?> years old <?cs else ?> died at the age of <?cs var person.age ?> <?cs /if ?>. </p>'; // parse the template cs_parse_string($cs, $template); // render the template print cs_render($cs); // destroy the resources hdf_destroy($hdf); cs_destroy($cs);
これを実行すると、
$ php clearsilver02.php <p> <b>日本語 "<i>ソフト</i>" まだ残って高</b>, the greatest ①㈱ of them all, died at the age of 71 . </p>$
のように出力されます。tracのabout.csに対して
<?php $hdf = hdf_init(); $cs = cs_init($hdf); hdf_set_value($hdf, 'about.page', 'config'); $template = file_get_contents('about.cs'); cs_parse_string($cs, $template); print cs_render($cs); hdf_destroy($hdf); cs_destroy($cs);
のようにやってみましたが、問題なく出力されました。
$ php clearsilver03.php <div id="ctxtnav" class="nav"> <h2>About ナビゲーション</h2> <ul> <li class="first last"><a href="">Overview</a></li> </ul> </div> <div id="content" class="about_config"> <h1>Configuration</h1> <table><thead><tr><th class="section">Section</th> <th class="name">Name</th><th class="value">Value</th></tr></thead></table> <div id="help"> コンフィグについては、 <a href="/TracIni">TracIni</a> を参照して下さい。 </div> </div> $
あとはテンプレートの書き方次第ですが、ClearSilver Documentation辺りを参照してくださいw
興味があったのはCソースの方で約900行ありますが、ZEND_FETCH_RESOURCEでresourceを取り出してゴニョゴニョやってるのがほとんどなので、勉強がてら読んでみようかと思います。
あ。拡張勉強会のコードリーディングネタに良いかも。。。?