¿Cómo instalo el motor de plantilla Twig?
-
26-09-2019 - |
Pregunta
Estoy ejecutando MAMP como mi servidor local. He instalado ramita en /Applications/MAMP/svn/twig/twig/lib
. He incluido esta ruta en mi archivo php.ini:
include_path = ".:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/svn/zendframework/trunk/library:/Applications/MAMP/svn/twig/twig/lib";
¿Qué debe entrar en mi carpeta HTDOCS para que complete la twig de instalación y acceso?
Solución
No necesita instalar nada, puede usarlo en PHP. Aquí hay un script simple para cargar y representar una plantilla:
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!") );
Tu plantilla.tpl podría verse así:
<html>
<!-- ... -->
<body>
<h1>{{ msg|e }}</h1>
</body>
</html>
Este ejemplo solo escapará y se eco de "hola, mundo".
Para más información, solo lea la documentación para (PHP) Developerpers y diseñadores de plantillas.
Otros consejos
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'));
Encuentra más información sobre esto Tutorial
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow