题
我正在运行MAMP作为本地服务器。我已经安装了树枝 /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";
为了使我完成安装和访问树枝,我需要进入我的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>
这个示例只会逃脱并回应“你好,世界”。
其他提示
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