简体   繁体   中英

How do I install the Twig Template Engine?

I am running Mamp as my local server. I Have installed Twig in /Applications/MAMP/svn/twig/twig/lib . I have included this path in my php.ini file:

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

What needs to go into my htdocs folder in order for me to complete the installation and access Twig?

You don't need to install anything, you can just use it in PHP. Here's a simple script to load and render a template:

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!") );

Your template.tpl could look like this:

<html>
    <!-- ... -->
    <body>
        <h1>{{ msg|e }}</h1>
    </body>
</html>

This example will just escape and echo "Hello, World".

For more information just read the documentation for (PHP) developpers and template designers .

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

find more info on this Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM