Twigテンプレートエンジンをインストールするにはどうすればよいですか?
-
26-09-2019 - |
質問
私はローカルサーバーとしてMAMPを実行しています。 Twigをインストールしました /Applications/MAMP/svn/twig/twig/lib
. 。私は私のphp.iniファイルにこのパスを含めました:
include_path = ".:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/svn/zendframework/trunk/library:/Applications/MAMP/svn/twig/twig/lib";
インストールを完了してTwigにアクセスするには、HTDOCSフォルダーに移動する必要がありますか?
解決
何もインストールする必要はありません。PHPで使用できます。テンプレートをロードしてレンダリングする簡単なスクリプトを次に示します。
require_once( "Twig/Autoloader.php" );
Twig_Autoloader::register();
// Load template files from the ./tpl/ folder and use ./tpl/cache/ for caching
$twig = new Twig_Environment( new Twig_Loader_Filesystem("./tpl"),
array( "cache" => "./tpl/cache" ) );
// Load and render 'template.tpl'
$tpl = $twig->loadTemplate( "template.tpl" );
echo $tpl->render( array("msg"=>"Hello, World!") );
Template.tplは次のようになります。
<html>
<!-- ... -->
<body>
<h1>{{ msg|e }}</h1>
</body>
</html>
この例は逃げて「こんにちは、世界」を反映します。
詳細については、ドキュメントをお読みください (PHP)開発者 と テンプレートデザイナー.
他のヒント
include __DIR__ . "/vendor/twig/twig/lib/Twig/Autoloader.php";
//register autoloader
Twig_Autoloader::register();
//loader for template files
$loader = new Twig_Loader_Filesystem('templates');
//twig instance
$twig = new Twig_Environment($loader, array('cache' => 'cache'));
//load template file
$template = $twig->loadTemplate('index.html');
//render a template
echo $template->render(array('title' => 'Welcome to Twig template'));
これの詳細を見つけてください チュートリアル
所属していません StackOverflow