不使用 Symfony 的 TwigSymfony、Twig

2023-09-07 00:25:03 作者:丧心病狂的胖子

我没有找到任何关于在没有 Symfony 的情况下使用 twig 的信息.我想在没有 symfony 框架的情况下在我的自定义网页中使用 twig.有可能吗?

I don't find any information about using twig without Symfony. I want to use twig in my custom web page without symfony framework. Is it possible?

推荐答案

给你一个可运行的示例:

To give you a runnable sample:

在空项目中安装 Twig:

Install Twig in empty project:

 composer require "twig/twig:^3.0"

iPhone5新Lightning接口让配件厂商苦不堪言

创建以下test.php";文件:

Create the following "test.php" file:

<?php

  require_once('vendor/autoload.php');

  $loader = new TwigLoaderFilesystemLoader('./templates');
  $twig = new TwigEnvironment($loader);

  echo $twig->render('demo.twig', ['name' => 'Fabien']);

创建视图:

Create the view:

 mkdir templates
 cd templates
 echo "Hello, {{ name }}!" > demo.twig

运行演示:

Run the demo:

 cd ..
 php test.php