2010-08-08 108 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