Comment installer le moteur du modèle de brindille?
-
26-09-2019 - |
Question
J'exécute MAMP en tant que serveur local. J'ai installé des brindilles /Applications/MAMP/svn/twig/twig/lib
. J'ai inclus ce chemin dans mon fichier php.ini:
include_path = ".:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/svn/zendframework/trunk/library:/Applications/MAMP/svn/twig/twig/lib";
Qu'est-ce qui doit accéder à mon dossier HTDOCS afin que je puisse terminer l'installation et accéder à des brindilles?
La solution
Vous n'avez pas besoin d'installer quoi que ce soit, vous pouvez simplement l'utiliser en PHP. Voici un script simple pour charger et rendre un modèle:
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!") );
Votre modèle.tpl pourrait ressembler à ceci:
<html>
<!-- ... -->
<body>
<h1>{{ msg|e }}</h1>
</body>
</html>
Cet exemple s'échappera et échappera "bonjour, monde".
Pour plus d'informations, lisez simplement la documentation pour (PHP) Developpers et concepteurs de modèles.
Autres conseils
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'));
Trouvez plus d'informations à ce sujet Didacticiel
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow