How do I install the Twig Template Engine?
-
26-09-2019 - |
문제
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
제휴하지 않습니다 StackOverflow