将项目的模板引擎从 savant2 更改为 plates

Changing the template engine of a project from savant2 to plates

我在将使用 savant2 模板引擎的旧项目转换为 plates 模板引擎时遇到问题,我已经通过 platesphp文档,它仍然令人困惑。 savant2模板中的项目是这样构造的(示例),第一个文件

$savant = new Savant2();
$savant->addPath('template', [LINK TO TEMPLATE OR THEME]);

然后在其他文件中声明需要的变量

global $savant;
$my_name = "Victor";
$savant->assign('name', $my_name);
$savant->display('include/header.tmpl.php');

然后,在header.tmpl.php文件中

<?php echo $this->name; ?>

现在,我想使用 plates 模板引擎代替 savant2,这就是我的代码现在的结构,第一个文件

$plates  = new League\Plates\Engine();
$plates->addFolder('template', [LINK TO TEMPLATE OR THEME]);

在另一个文件中,

global $plates;
$my_name = "Victor";
$plates->addData('name', $my_name);
$plates->render('include/header.tmpl.php');

然后,在 header.tmpl.php 文件中

<?=$this->e($name)?>

虽然它没有按预期工作,但我的困惑在于使用 render、addData 和 addFolder 产生与 savant2 相同的结果

我终于解决了

    //Location of savant2 library
      require('/Savant2/Savant2.php');
    // set default template paths: 
      $savant = new Savant2();
      $savant->addPath('template', '/themes/');

替换上面的,然后添加这个

    // Enable the composer autoload file (Depending on how your system is set up)
     require_once '/vendor/autoload.php';
     $plates = League\Plates\Engine::create('/themes/', 'tmpl.php');

然后对于下面的 savant2 实现

    require 'config.php';

    $name = 'Victor Alagwu';
    $school = 'University of Nigeria, Nsukka';
    $course = 'Computer Science';
    $savant->assign('author', $name);
    $savant->assign('school', $school);
    $savant->assign('course' $course);
    $savant->display(home.tmpl.php);

将其替换为此板实施

    require 'config.php';
    $name = 'Victor Alagwu';
    $school = 'University of Nigeria, Nsukka';
    $course = 'Computer Science';

    plate['name'] = $name;
    plate['school'] = $school;
    plate['course'] = $course;

    echo $plates->render('home.tmpl.php', $plate);

然后是模板文件 (Savant2)

       Name:
       <?php echo $this->name; ?>
       Course:
       <?php echo $this->course; ?>
       School: 
       <?php echo $this->school; ?>

替换为以下(对于板)

    Name:
     <?php echo $name; ?>
    Course:
    <?php echo $course; ?>
    School: 
    <?php echo $school; ?>

你有以前的 savant2 应用程序,现在 运行 在 plates 模板引擎上