1

我在将使用savant2模板引擎的旧项目转换为 Plate 模板引擎时遇到问题,我已经阅读了 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 相同的结果

4

1 回答 1

1

我终于解决了

    //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 应用程序,现在在板块模板引擎上运行

于 2018-07-31T20:26:24.237 回答