2010-08-08 106 views
1

我正在运行Mamp作为我的本地服务器。我已经在/Applications/MAMP/svn/twig/twig/lib上安装了Twig。我已经包含在我的php.ini文件路径:如何安装Twig模板引擎?

include_path = ".:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/svn/zendframework/trunk/library:/Applications/MAMP/svn/twig/twig/lib"; 

什么需要进入我的htdocs文件夹,以便我完成安装和接入的树枝?

+2

我可以问你为什么你会使用,当你已经在使用Zend框架枝杈更多信息?我建议你远离PHP模板引擎。 [这是为什么](http://stackoverflow.com/questions/2235179/lightweight-php5-based-template-class-system/2235196#2235196)。 – quantumSoup 2010-08-08 23:50:45

+0

我只是在做一个需要模板引擎的教程。感谢您的信息,虽然...... – demet8 2010-08-09 00:13:49

回答

12

你不需要安装任何东西,你可以在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) developperstemplate designers

+0

谢谢这有助于澄清一些事情。 – demet8 2010-08-09 13:45:57

0
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')); 

找到这个Tutorial