特長
・高速
プレーンな最適化されたPHPコードに変換されるからオーバーヘッドが超少ない!
・セキュア
サンドボックスモードがあって信頼出来ないコードも安全に実行できる (利用者がテンプレートデザインを変更できる場合とか)
・フレキシブル
柔軟なlexer(?)とパーサで動く。カスタムタグとフィルターとDSLを作ることができる
ウハー!曖昧(・へ・)
インストール
方法が色いろある
http://twig.sensiolabs.org/doc/intro.html#installation
とりあえずいつもどおりPEARで。
こうして、
pear channel-discover pear.twig-project.org
こうすると、
pear install twig/Twig
こうなる。
downloading Twig-1.11.1.tgz ...
Starting to download Twig-1.11.1.tgz (76,492 bytes)
.................done: 76,492 bytes
install ok:
channel://pear.twig-project.org/Twig-1.11.1
これで
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Twig/Autoloader.php'; | |
Twig_Autoloader::register(); | |
$loader = new Twig_Loader_String(); | |
$twig = new Twig_Environment($loader); | |
echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien')); | |
?> |
こうなって
Hello Fabien!
これで
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Twig/Autoloader.php'; | |
Twig_Autoloader::register(); | |
$loader = new Twig_Loader_Filesystem('template/'); | |
$twig = new Twig_Environment($loader, array( | |
'cache' => 'cache/', | |
)); | |
echo $twig->render('hello.html', array('message' => 'HELLO!!')); | |
?> |
こうなった。
<!DOCTYPE html>
<html>
<head>
<title>:-)</title>
</head>
<body>
HELLO!!
</body>
</html>
かねこ<(゚ε゚)>